A Dart dependency injection library aimed to be flexible, predictable and easy to use.

Overview

dino

Dino is a Dart dependency injection library with optional code generation. It was inspired by DI in .NET and aimed to be flexible, predictable and easy to use.

Quick start

It is assumed that you have a basic understanding of dependency injection.

Suppose we have multiple services dependent on each other:

class Repository {
  Repository(this.restClient);
  final RestClient restClient;

  Future<void> sendMessage(String message) async {
    try {
      await restClient.sendMessage(message);
    } catch (e) {
      print('Error sending message: $e');
    }
  }
}

class RestClient {
  RestClient(this.dio);
  final Dio dio;

  Future<void> sendMessage(String message) async {
    await dio.post('/api/message', data: {'message': message});
  }
}

Then their registration in dino will look like this:

void main() {
  final ServiceCollection services = RuntimeServiceCollection();

  services.addInstance(Dio());

  services.addSingletonFactory(
    (sp) => RestClient(
      dio: sp.getRequired<Dio>(),
    ),
  );

  services.addSingletonFactory(
    (sp) => Repository(
      restClient: sp.getRequired<RestClient>(),
    ),
  );
}

If we add code generation using dino_generator, then the code will become even nicer:

import 'main.dino.g.dart';

void main() {
  final ServiceCollection services = $ServiceCollection();

  services.addInstance(Dio());
  services.addSingleton<RestClient>();
  services.addSingleton<Repository>();
}

Now we can use registered services:

final rootScope = services.buildRootScope();

final repository = rootScope.serviceProvider.getRequired<Repository>();
repository.sendMessage('Hello world!');

You can also use dino in flutter with dino_flutter package:

void main() {
  final ServiceCollection services = $ServiceCollection();

  services.addInstance(Dio());
  services.addSingleton<RestClient>();
  services.addSingleton<Repository>();

  final rootScope = services.buildRootScope();

  runApp(
    DinoProvider(
      serviceProvider: scope.serviceProvider,
      child: Application(),
    ),
  );
}

Then you can use the ServiceProvider from the BuildContext:

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final repository = context.serviceProvider.getRequired<Repository>();

    // build widget
  }
}

For a better understanding of concepts such as ServiceCollection, ServiceScope, and ServiceProvider, and to learn more about dino, you can check out the detailed documentation.

Documentation

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

You might also like...

Time Table application specifically aimed towards students. Share Time-Tables. Suggest Updates.

Time-Table-App Time Table application specifically aimed towards students. Tech stack Project is created by: Flutter: 2.8.1 Dart: 2.15.1 Planned Featu

Oct 7, 2022

An atomic state management library for dart (and Flutter). Simple, fast and flexible.

nucleus An atomic dependency and state management toolkit. Design goals Simplicity - simple API with no surprises Performance - allow for millions of

Jan 2, 2023

A fast, flexible, IoC library for Dart and Flutter

Qinject A fast, flexible, IoC library for Dart and Flutter Qinject helps you easily develop applications using DI (Dependency Injection) and Service L

Sep 9, 2022

Fluro is a Flutter routing library that adds flexible routing options like wildcards, named parameters and clear route definitions.

Fluro is a Flutter routing library that adds flexible routing options like wildcards, named parameters and clear route definitions.

English | Português The brightest, hippest, coolest router for Flutter. Features Simple route navigation Function handlers (map to a function instead

Jan 4, 2023

A zero-dependency web framework for writing web apps in plain Dart.

Rad Rad is a frontend framework for creating fast and interactive web apps using Dart. It's inspired from Flutter and shares same programming paradigm

Dec 13, 2022

Hyakunin-isshu - This library has been developed to make it easy to use Japanese Hyakunin Isshu in Dart and Flutter applications

Hyakunin-isshu - This library has been developed to make it easy to use Japanese Hyakunin Isshu in Dart and Flutter applications

1. About 1.1. What Is Hyakunin Isshu? 1.2. Introduction 1.2.1. Install Library 1

Jan 11, 2022

Dart anticaptcha - Easy to use library for solving captcha

The solution of the captcha is carried out at the expense of the resource worker

Jan 12, 2022

A flutter widget to indicate loading progress. Easy to use, easy to extend

💙 👾 💫 A flutter widget to indicate loading progress. Easy to use, easy to extend

May 30, 2022
Owner
null
[Flutter SDK V.2] - Youtube Video is a Flutter application built to demonstrate the use of Modern development tools with best practices implementation like Clean Architecture, Modularization, Dependency Injection, BLoC, etc.

[Flutter SDK V.2] - Youtube Video is a Flutter application built to demonstrate the use of Modern development tools with best practices implementation like Clean Architecture, Modularization, Dependency Injection, BLoC, etc.

R. Rifa Fauzi Komara 17 Jan 2, 2023
Raden Saleh 20 Aug 12, 2023
A simple dependency injection plugin for Flutter and Dart.

A super simple dependency injection implementation for flutter that behaviours like any normal IOC container and does not rely on mirrors

Jon Samwell 91 Dec 13, 2022
Arisprovider - A mixture between dependency injection (DI) and state management, built with widgets for widgets

A mixture between dependency injection (DI) and state management, built with wid

Behruz Hurramov 1 Jan 9, 2022
MVC pattern for flutter. Works as state management, dependency injection and service locator.

MVC pattern for flutter. Works as state management, dependency injection and service locator. Model View Controller Here's a diagram describing the fl

xamantra 115 Dec 12, 2022
Flutter getx template - A Flutter Template using GetX package for State management, routing and Dependency Injection

Flutter GetX Template (GetX, Dio, MVVM) This Flutter Template using GetX package

Tareq Islam 6 Aug 27, 2022
App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and less verbose.

App HTTP Client App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and le

Joanna May 44 Nov 1, 2022
Clean Architecture + TDD + SOLID + Dependency Injection + GitFlow + Mobx

Clean Architecture + TDD + SOLID + Dependency Injection + GitFlow + Mobx Flutter Interview Challenge This app is part of an interview process. It took

Vinicius Souza 13 Dec 28, 2022
Dart generic app exceptions, mainly aimed at flutter

Micro dart library that expose generic exceptions. The goal of this library is to for the end user to rethrow exceptions of other libraries with the s

cedvdb 2 Dec 31, 2022
This project is aimed to read online attendance by using QR code

Online Yoklama (Online Attandance) This project is aimed to read online attendance by using QR code. Materials ESP32 AI Thinker CAM PL2303 UART Buzzer

Emre ARIK 2 Jan 14, 2022