top of page
  • Writer's pictureRobin Alex Panicker

Basics of Flutter Modular


Basics of Flutter Modular

Flutter Modular is a package that helps you modularize your Flutter applications. It provides a way to divide your application into independent modules, each with its own set of routes, dependencies, and data. This can make your application easier to understand, maintain, and test.


In this blog we will explore the basics of Flutter Modular package and how to use it.


Why use Flutter Modular


There are many reasons why you might want to use Flutter Modular. Here are a few of the most common reasons:

  • To improve the readability and maintainability of your code. When your application is divided into modules, it becomes easier to understand how each part of the application works. This can make it easier to find and fix bugs, and to make changes to the application without breaking other parts of the code.

  • To improve the testability of your application. Modularization can make it easier to write unit tests for your application. This is because each module can be tested independently of the other modules.

  • To improve the scalability of your application. As your application grows in size and complexity, modularization can help you to keep it manageable. This is because each module can be developed and maintained by a separate team of developers.

How to use Flutter Modular


To use Flutter Modular, you first need to install the package. You can do this by running the following command in your terminal:

flutter pub add flutter_modular 

Once the package is installed, you can start creating your modules. Each module should have its own directory, which contains the following files:

  • module.dart: This file defines the module's name, routes, and dependencies.

  • main.dart: This file is the entry point for the module. It typically imports the module's routes and dependencies, and then creates an instance of the module's Module class.

  • routes.dart: This file defines the module's routes. Each route is a function that returns a Widget.

  • dependencies.dart: This file defines the module's dependencies. Each dependency is a class that is needed by the module.

Once you have created your modules, you can start using them in your application. To do this, you need to import the module's module.dart file. You can then use the module's routes and dependencies in your application's code.


For example, here is a basic module.dart file for a module named home:

import 'package:flutter_modular/flutter_modular.dart';

@module
abstract class HomeModule {
  @route("")
  Widget homePage();
}

This module defines a single route, /, which returns a Widget named homePage().


Here is an example of the main.dart file for the same module:

import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';

import 'routes.dart';

void main() {
  runApp(ModularApp(
    module: HomeModule(),
  ));
}

This file imports the module's routes.dart file, and then creates an instance of the module's Module class.


Finally, here is an example of the routes.dart file for the same module:

import 'package:flutter_modular/flutter_modular.dart';

@moduleRoute("/")
class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("Hello, world!"),
    );
  }
}

This file defines the module's homePage() route, which returns a Widget that displays the text "Hello, world!".


Once you have created your modules, you can start using them in your application. To do this, you need to import the module's module.dart file. You can then use the module's routes and dependencies in your application's code.


For example, here is how you would use the homePage() route from the home module in your application's main home.dart file:

import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';

import 'home_module/module.dart';

void main() {
  runApp(ModularApp(
    module: HomeModule(),
    child: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("My App"),
      ),
      body: Center(
        child: RaisedButton(
          child: Text("Go to home page"),
          onPressed: () {
            Modular.to.pushNamed("/home");
          },
        ),
      ),
    );
  }
}

This code imports the home_module/module.dart file, and then uses the Modular.to.pushNamed("/home") method to navigate to the home module's homePage() route.


Tips for using Flutter Modular

  • Use a consistent naming convention for your modules. This will make it easier to find and understand your code.

  • Use a separate module for each logical part of your application. This will help you to keep your code organized and maintainable.

  • Use dependency injection to share dependencies between modules. This will help you to decouple your modules and make them easier to test.

  • Use unit tests to test your modules independently of each other. This will help you to find and fix bugs early in the development process.

  • Use continuous integration and continuous delivery (CI/CD) to automate the deployment of your modules to production. This will help you to get your changes to production faster and more reliably.

Conclusion


Flutter Modular is a powerful tool that can help you to modularize your Flutter applications. By dividing your application into modules, you can improve the readability, maintainability, testability, and scalability of your code. If you are working on a large or complex Flutter application, then I highly recommend using Flutter Modular.


Happy coding!

Blog for Mobile App Developers, Testers and App Owners

 

This blog is from Finotes Team. Finotes is a lightweight mobile APM and bug detection tool for iOS and Android apps.

In this blog we talk about iOS and Android app development technologies, languages and frameworks like Java, Kotlin, Swift, Objective-C, Dart and Flutter that are used to build mobile apps. Read articles from Finotes team about good programming and software engineering practices, testing and QA practices, performance issues and bugs, concepts and techniques. 

bottom of page