Architect in Flutter project uses BLOC pattern P1

Tram Ho

Today, I will do a small demo of the architect of the Flutter project, you can maintain and test it easily.

1. Why do we need an architect in the project?

When I first got into programming, I was only interested in the output and efficiency of the program. I have never structured a program or project that I write.When I come across more projects, projects need to develop new features, fix features, I usually take longer to fix and sometimes also add some bugs to the project. The main cause of all these problems is that I have not followed an architect model. Today, I’m going to start a project to introduce architect in Flutter.

2 Objectives

I will build a project with a girl list screen of items. These items will be taken from the server and taken from the Movie adverb.

3. The architect diagram

Diagram shows how to get data to Data layer and vice versa. Bloc will never refer to the Widget in UI Screen. Screen Screen only observes changes from Bloc class. I will give some Q&A to better understand diagram:

  1. What is Bloc Pattern? A: It uses state management and accesses data from a central location in Flutter.
  2. Is this architecture like any other architecture. TL: Same as MVP, MVVM. The only difference is that BLOC will replace ViewModel in MVVM.
  3. What is under the guise of Bloc? Something essential for state management in one place. TL: STREAMS or REACTIVE is one approach. Data will be transferred from BLOC to UI or from UI to BlOC in stream form. If you haven’t heard about streaming, you can read this post

Start project on Bloc pattern.

  1. Create a Project Flutter named “myProjectName”.
  2. Write the following code in the main.dart file:

3. Create the app.dart file:

  1. Create a new package with the names resources, blocs, models, ui .

  • package blocs will contain files related to BLOC.
  • Package models will contain POJO (Plain Old Java Object) class or model class of Json response get from server.
  • The resources package contains the repository classes and makes network calls.
  • package ui contains list of screens shown to the user.
  1. Add the Rxdart library to the pubspec.yaml file

and perform the installation by clicking “Pub get” if you are using Android studio or you type the command in terminal (Mac), command (Win):

  1. We are done with the project framework. Now let’s deal with the network layer, understand the API endpoint. You perform login to the page and get API key from Setttings page. We will access the following url to get the data:

Response Data:

  1. Now let’s create a model or POJO class for this type of response. Create the file item_model.dart in package models. Copy the following code:

  1. Create file movie_api_provider.dart in package resources to implement the network implementation. Copy and page the following code into the file:

Note: Please create an account from https://www.themoviedb.org/ and get apikey instead of ‘your_api_key’.

The fetchMovieList method is used to make the API call. When the network call completes it will return a Future ItemModel object if the network call is successful or an exception.

  1. Next we will create repository.dart into resource . Copy and page the following code into the file:

We will import the movie_api_provider.dart file and call fetchMovieList (). Repository class is the central point from which data will be transferred to BLOC .

  1. Now for a more complicated part, implement the logic bloc. Let’s create movies_bloc.dart in package blocs . Please copy and page to the movies_bloc.dart file, I will explain in detail:

Inside the MoviesBloc class, we are creating the Repository class object which will be used to access the fetchAllMovies ()

In the MoviesBloc class, we create an object class Repository used to access fetchAllMovies () by creating a PublishSubject object which is responsible for adding the data it receives from the server as an ItemModel object and converting it to the UI screen as a stream. . To convert ItemModel object as stream we create a method allMovies () whose return type is Observables. Using RxDart makes updating data from server to monitor easy. The UI screen will always observe the MoviesBloc class to listen for changes and update if any.

  1. At the end of the article, copy the following code to the movie_list.dart file, this file is created from the UI package :

The above code is not using StatefulWidget, instead will use StreamBuilder instead of updating UI.

Here is the complete code, please refer to it: https://github.com/SAGARSURI/MyMovies .

References:

https://medium.com/codechai/architecting-your-flutter-project-bd04e144a8f1

Share the news now

Source : Viblo