DEV Community

Mattia Pispisa
Mattia Pispisa

Posted on

Flutter Monorepo & Dependency Injection

I described dependency inversion and a way to use in dart/flutter in this article. In this post I will use the same concepts and libraries.


Monorepo refers to the practice of software development in which all components of an application or system are maintained within a single repository. A good article that describes well what it is, pros and cons is the following:
In the case of a flutter application an excellent monorepo manager is Melos, here a tutorial on how to create and manage it.

A concrete use case


Suppose our application is a social app and we need to manage posts and users. The application is divided into packages where each package contains views and logics to manage a specific section of the application (users, posts, ...). Route is defined within the core package. At this point each package defines its routes which will then be displayed in the sidebar (see uml). The result is as follows:

  • The abstract class Route is in core;
  • Its implementations are in different packages (PostRoute, UserRoute, ...).


This can be the folder structure,

  - lib
  - pubspec.yaml

  - core
      - pubspec.yaml
  - user
      - pubspec.yaml
  - post
      - pubspec.yaml
Enter fullscreen mode Exit fullscreen mode

where the various pubspec.yaml will look like:

## app
    path: ../modules/core
    path: ../modules/user
    path: ../modules/post
Enter fullscreen mode Exit fullscreen mode
## user
    path: ../core
Enter fullscreen mode Exit fullscreen mode
// app code
@Injectable(as: Navigation)
class AppNavigation implements Navigation {
  List<Route> routes = [UsersRoute()];

// core code
class SideBarMenu extends StatelessWidget {
  const SideBarMenu(this.navigation);

  final Navigation navigation;

  Widget build(BuildContext context) {
    return Column(
      children: navigation.routes
            (r) => Link(
              url: r.url,
              child: Text(,

abstract class Navigation {
  List<Route> get routes;

abstract class Route {
  String get url;
  String get name;
  Widget view();

// post package
class PostsRoute implements Route {
  String url => "posts";
  String name => "Posts";
  Widget view => Container();
Enter fullscreen mode Exit fullscreen mode

Each package defines a micropackage (core, user, posts). Additionally in core we define getIt and export it for the other modules.

// only in core
GetIt getIt = GetIt.instance;

// every package
initMicroPackage() {} 
Enter fullscreen mode Exit fullscreen mode

In app we init the injection and register the various micro packages.

    initializerName: 'init',
    preferRelativeImports: true,
    asExtension: false, 
    // packages
    externalPackageModulesBefore: [
FutureOr<void> configureInjection() => init();
Enter fullscreen mode Exit fullscreen mode

In this case all packages were registered (in order) with the method externalPackageModulesBefore but there are three properties that can be used (in order of registration): 1. externalPackageModulesBefore, 2. externalPackageModules, 3. externalPackageModulesAfter.


This concludes the article on Monorepo & Dependency Injection.

For more information on micropackage I recommend reading injectable package.

Top comments (0)