Architecturing Flutter Application with MVVM using GetIt

Overview

Architecturing Flutter Application with MVVM using GetIt

Starting with Basics πŸ§‘β€πŸ’»

  • Flutter:

Flutter is an open-source framework by Google for building beautiful, natively compiled, multi-platform applications from a single codebase.

Features of Flutter:

  1. Fast: Flutter code compiles to ARM or Intel machine code as well as JavaScript, for fast performance on any device.

  2. Productive: Build and iterate quickly with Hot Reload. Update code and see changes almost instantly, without losing state.

  3. Flexible: Build and iterate quickly with Hot Reload. Update code and see changes almost instantly, without losing state.

  • GetIt:

As your App grows, at some point you will need to put your app's logic in classes that are separated from your Widgets. Keeping your widgets from having direct dependencies makes your code better organized and easier to test and maintain. But now you need a way to access these objects from your UI code.

This is where GetIt comes into play. GetIt is a simple Service Locator for Dart and Flutter projects. It can be used instead of Inherited Widget or Provider to access objects from your UI.

Since it is not a state management solution. It's a locator for your objects, so in some other way you need to notify your UI about changes like Streams it ValueNotifier.

Many of us get confused about how to use notify UI, and how to integrate it with MVVM Architecture. This tutorial will help you set up architecture to boilerplate for your big projects.

  • MVVM - (Model-View-ViewModel)

MVVM is a very popular architectural pattern when it comes to software development, to separate out presentation layers with business logic, and bind them together with ViewModels.

Features of MVVM:

  1. Easy to Maintain: The presentation layer and Business Logic layer are separated, due to which the code is easily maintainable and you can reuse the code or widgets. This may not seem useful for a short project. But you will see its power when creating a big project, where you constantly need to use reuse the code.

  2. Easy to Test: Since all the function logic is written in a separate file, it is easier to unit test for the functions, than event-driven code.

I guess that's enough for the basics, lets's see some cool stuff. 😎

  • Directory Structure

structure.png

Let's go through all the sections one by one-

  • config: contains the configuration of the app, for a responsive application.
  • constant: directory to store app constants, such as any common textStyle, flutter_toasts, etc.
  • enum: directory to store enumeration of app, here it contains only one Enums for now.
enum ViewState { Idle, Busy }

This Enum identifies when the app is contacting servers or DB, and then it can be helped to show ProgressIndicator on UI, according to ViewState.

  • provider: This directory contains some important configs regarding the setup of GetIt, registering ViewModel with GetIt, and ValueNotifier for notifying changes to UI.
GetIt getIt = GetIt.instance;
void setupLocator() {
  getIt.registerLazySingleton(() => NavigationService());
  getIt.registerFactory(() => ApiService());
  getIt.registerFactory(() => HomeScreenViewModel());
}
  • services: If You are using FIrebase for the app, then you can skip this directory, otherwise if you are planning to use your own server, like Express, Django, Flask. This directory will be very useful, as it contains methods for RESTFUL Apis, converting response to easy-to-understand class ApiResponse, etc. You make take a look at it inside it.

  • src: It will contain all your source files. It is briefly divided into 3 sections:

    1. models: To store your business classes.
    2. screens: To store all your UI screens.
    3. widgets: To store reusable components.

Let's go over the last two files, which will be the entry points of our app.

  1. main.dart
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  setupLocator();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: getIt<NavigationService>().navigatorKey,
      title: 'Flutter MVVM',
      initialRoute: '/',
      onGenerateRoute: RouteGenerator.generateRoute,
    );
  }
}
  1. route_generator.dart
class RouteGenerator {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    // Getting arguments passed in while calling Navigator.pushNamed
    final args = settings.arguments as dynamic;

    switch (settings.name) {
      case '/':
        return MaterialPageRoute(builder: (_) => const HomeScreen());
      default:
        // If there is no such named route in the switch statement, e.g. /third
        return _errorRoute();
    }
  }

Kudos to you for a long way down here. Let's run the app ! πŸŽ‰

app.gif

That looks the same as starting of new flutter project. So what are we doing extra here?

Let's go through the important files, to understand the underlying process.

  • lib/src/screens/home_screen.dart This is a presentation layer, i.e. UI Screen, where you do your designing. Notice we wrapped Scaffold with BasView Builder of type HomeScreenModel. This binds our UI, with ViewModel, and notifies UI, when any changes occur.
import 'package:flutter/material.dart';
import 'package:flutter_mvvm_with_getit/provider/base_view.dart';
import 'package:flutter_mvvm_with_getit/view/home_screen_viewmodel.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return BaseView<HomeScreenViewModel>(
      builder: (context, model, _) => Scaffold(
        appBar: AppBar(
          title: const Text("Flutter MVVM"),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Text(
                'You have pushed the button this many times:',
              ),
              Text(
                model.counter.toString(),
                style: Theme.of(context).textTheme.headline4,
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () => model.incrementCounter(),
          child: const Icon(Icons.add),
        ),
      ),
    );
  }
}
  • lib/src/view/home_screen_viewmodel.dart

This file is our ViewModel Layer, which is bonded by UI. Here we write the logic of our function, and any changes to variables, are notified to UI, and UI rebuilds accordingly.

Notice, we have a variable counter, which is used in home_screen, and a method to increase counter is also used in home_screen.

import 'package:flutter_mvvm_with_getit/provider/base_model.dart';

class HomeScreenViewModel extends BaseModel {
  int counter = 0;

  void incrementCounter() {
    counter++;
    notifyListeners();
  }
}

On this last line, there is a function - notifyListeners, this function notifies UI that some variable is changed, and rebuild your UI, according to the changes.

That's it, folks, for Archutecturing Flutter Application with MVVM Architecture using GetIt.

I hope you will be able to learn something new from this tutorial and will create something fantastic with this boilerplate.

I am also attaching Github Repo, which you may fork it, and use as a boilerplate for your application. This Repo will have all the important folders and files that you will use while creating any large application.

Still Doubt? Let me know in comments section ;)

Github Repo: Flutter MMVM with GetIt

Connect with me: Linkedin

You might also like...

A basic boilerplate template for starting a Flutter GetX project. GetX, Dio, MVVM, get CLI, Localization, Pagination etc are implemented.

A basic boilerplate template for starting a Flutter GetX project. GetX, Dio, MVVM, get CLI, Localization, Pagination etc are implemented.

Flutter GetX Template (GetX, Dio, MVVM) This Flutter Template using GetX package for State management, routing and Dependency Injection (bindings). We

Jan 9, 2023

Flutter plugin to rapidly create a Page with MVVM design pattern

mvvm_builder mvvm_builder is a Flutter plugin to help you implement MVVM design pattern with flutter. MVVM = Model - View - ViewModel Installation To

Jun 4, 2021

This is a practical guide to MVVM architecture in flutter with riverpod for state management.

This is a practical guide to MVVM architecture in flutter with riverpod for state management.

flutter_mvvm This project is a practical guide to MVVM pattern in flutter with Riverpod. Install flutter Get your APIKEY from https://api.openweatherm

Jan 1, 2023

Flutter GetX Template (GetX, Dio, MVVM)

Flutter GetX Template (GetX, Dio, MVVM)

Flutter GetX Template (GetX, Dio, MVVM) This Flutter Template using GetX package for State management, routing and Dependency Injection (bindings). We

Dec 18, 2022

This is a simple client of Reddit built with MVVM and Provider powered by a custom OAuth2.0 login

πŸ‘· πŸ”§ πŸ”© Flutter Starter Architecture (MVVM + Hive) My custom starter project for Flutter apps. I was looking for a simple way to build Flutter app in

Oct 26, 2022

Bu projede basit MVVM mimarisini kullandΔ±m

demo_movie_mvvm A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started

Dec 28, 2021

This is a simple client of Reddit built with MVVM and Provider powered by a custom OAuth2.0 login

πŸ‘· πŸ”§ πŸ”© Flutter Starter Architecture (MVVM + Hive) My custom starter project for Flutter apps. I was looking for a simple way to build Flutter app in

Oct 26, 2022

Blog app v2 Clean Architecture With MVVM

Blog app v2 Clean Architecture With MVVM

blog_app_v2 An initial blog app on my repo refactored and built with clean archi

Nov 6, 2022
Owner
ASMIT VIMAL
ASMIT VIMAL
A Flutter MVVM provider demo application.

Flutter MVVM + Provider Demo A Flutter MVVM provider demo application. Demo About It simply loads Posts data from API and render the posts on the scre

Shubham Chhimpa 132 Oct 17, 2022
Music player application for android. It's uses MVVM architecture and Provider & ValueNotifier state management.

music-player-flutter Flutter music player application which is my personal project published to play store. Project structures are as following,

null 30 Jul 10, 2022
Screener: a sample application Uses MVVM pattern

screener This is a sample application. Uses MVVM pattern Tries to encourage the use of boundaries (by using the concept of packages) Getting Started ?

Behruz Hurramov 0 Dec 27, 2021
Flutter app using MVVM architecture pattern , dio , provider , intl packages.

News App Simple news application using free news API for fetching realtime data from the internet. Features -Used MVVM architecture pattern. Packages

null 3 Mar 25, 2022
πŸš€ User management app built in flutter using clean architecture, MVVM, get it, dio, RxDart, bloc, cubit, getX and provider

?? Go Rest app In this project, we are going to build a user management app using Flutter. We have used the Go REST API to make HTTP request methods.

Sina 99 Aug 14, 2023
Menaclub app for admin using nodejs as backend,RestAPI,provider as statemangement and follows MVVM architecture

menzclub_admin A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started i

Pranav Pv 8 Nov 7, 2022
Touranment Manager app using Firebase ,provider and MVVM Architecture

tournament_lotter A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you starte

Pranav Pv 10 Nov 25, 2022
Flutter Architecture Blueprints is a project that introduces MVVM architecture and project structure approaches to developing Flutter apps.

Flutter Architecture Blueprints Flutter Architecture Blueprints is a project that introduces MVVM architecture and project structure approaches to dev

Katsuyuki Mori 2 Apr 9, 2022
Flutter Architecture Blueprints is a project that introduces MVVM architecture and project structure approaches to developing Flutter apps.

Flutter Architecture Blueprints Flutter Architecture Blueprints is a project that introduces MVVM architecture and project structure approaches to dev

Daichi Furiya 1.5k Dec 31, 2022
Boilerplate for Flutter + GetX MVVM

flutter_getx_boilerplate A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you

Olayemii Garuba 4 May 31, 2022