A Beginner’s Guide To Implementing MVVM Architecture In Flutter

A Beginner’s Guide To Implementing MVVM Architecture In Flutter

There was a time in the tech space, where businesses and companies struggled developing an app for their idea. This was primarily because they had to develop the same idea twice as applications – one for the Android ecosystem another one for the iOS ecosystem.

Financially and logistically, this was straining as companies had to hire two dedicated teams to work on the app development process or increase their time to market by hiring one team and wait until one of the applications got developed for the other one to start.

All of this changed in May 2017, when Google rolled out a revolutionary language that allowed developers to develop an app just once and use it across multiple platforms. That platform is what we call Flutter and Flutter application development has been a fan favorite among the developer community.

Though there are tons of features to rave about with Flutter application development, what appears to be the most prominent feature is its MVVM architecture.

As an experienced developer at a mobile application developing company, I have penned this article to briefly look at the model and understand how to implement MVVM architecture in Flutter.

What Is MVVM Architecture?

The MVVM pattern provides the distribution of data and UI which gives benefits such as flexibility and reusability of the code as well as data.

MVVM stands for Model, View, and View Model. The design pattern of MVVM is to build a view model that can represent the data through a view.

MVVM and Architecture:-

MVVM Architecture

In the above diagram of MVVM, you could see that the view model occupies the central position, which sends and receives data from the model and provides data to the view. It observes the changes in the data and responds accordingly using the model to the view.

The Model:-

The Model in the MVVM presents real-time data that will be used in application development. The Model only reflects the actual data and not the characteristics or any feature related to the application.

It means you can’t manipulate the way the data will be represented or formatted. Each item in the dataset will be presenting its own model when the data is fetched. The main Model is kept away from the logic part for neat code but sometimes, it includes the validation logic as well.

The View: –

The View in the MVVM design pattern presents the user interacting with the application. The View is a set of libraries to present the data more accurately and appropriately.

The View possesses the property to utilize behaviours associated with the Model such as identifying and acting according to user input. The View handles the actions done by a user, which can be manipulated by the Model’s functionalities.

The View Model:-

The most essential element of MVVM architecture is that the View Model occupies a central position that sends and receives the data from the model and provides the data to the View. Besides, it also observes the data changes and responds accordingly using the model to the view.

The View Model holds the actual data and the View part holds the formatted or updated data while keeping the controller as an interface between them.

MVVM implementation:-

MVV implementation is simple. All you have to do is follow the steps below:

  1. Create a new project.
  2. Get Dependencies
  3. Create the Model.
  4. Create UI.
  5. Create a Web Service.
  6. Create a view model.
  7. Create a widget.
  8. Create main.dart.

Create a new project

Create a new Flutter app. Create a New mvvm_example and remove all the code as we will add a custom main.dart.

Get Dependencies

Add the latest version of HTTP/dio and Provider package under the dependencies in pubspec.yaml file.

// Use the latest dependencies version from pub.dev

Create the Model

We will be building the latest News Article app, where the model will be the NewsArticle. In your “lib” folder, create a new folder and name its models. Inside the model folder, create a dart file and name it “NewsArticle.dart”

Create UI

Create a new folder in your lib folder and name it “screens.” Create a “dart” file and name it “NewsScreen.dart”. Enter the following code in it:

Create a Web Service

Create a new folder in your lib folder and name it “services.” Create a “dart” file and name it “WebServices.dart”. For the web service, we will use an API endpoint to newsapi.org.

Create a view model

In your “lib” folder, create another folder and name it “viewmodels”. In this folder, we will create two files:  one will be the “NewsArticleViewModel,” which will have the following content:

Create a new class called “NewsArticleViewModel” that will take in the model “NewsArticle” as a parameter and return the values from that model inside the View Model.

The next file will be NewArticleListViewModel” and will have the following code:

Create a widget.

Widget creator will be inside a new folder. We will create a widget in the “lib” folder. Inside this folder, create a file and name it “NewsGrid.dart”. This is just to continue with the MVVM reusability of the code. Here is the code for it:

Create main.dart

Finally, we will create the main.dart file:

ChangeNotifierProvider is defined to listen to a ChangeNotifier to expose the instance to it’s rebuild dependents whenever ChangeNotifier.notifyListeners is called.

The NewsArticleViewModel(data) is of type NewsArticle, an instance of which will be provided at the top level widget in main.dart

The parent widget is wrapped in ChangeNotifierProvider with the datatype of the ChangeNotifier NewsArticleViewModel (Data) class.

This includes a builder method that returns an instance of the ChangeNotifier ‘NewsArticleViewModel(data)’ class, which is used to expose the instance to all the children.

Wrapping Up

So, this is how you implement MVVM architecture in Flutter. Flutter mobile application development is seamless when you take a systematic approach. We’re sure this post was resourceful to you. If you have been looking for a profound mobile application development company, reach out to us now!

The following two tabs change content below.

Leave a Reply

Your email address will not be published. Required fields are marked *