vader_flutter 1.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 64

vader_flutter #

A Flutter implementation of the Vader package

Getting Started #

This is a Flutter package with helper classes to more convenient integration with Vader - runtime dependency injection library for Dart.

It's wrap around Provider package. And provides ModuleInjector and DependentWidget Widgets to manage your project dependencies.

And you can ask Why not just using a Provider? The first, Provider is a great package for maintain reactive interactions between different widgets. And I strongly recommend using it for that purpose. But using it just for a Dependency Injection pattern is resulting in writing too much code, because of Widget nature of Provider. So in my projects I need a fast and convenient way to define a lot of services, repositories, BloCs(Business Logic Components), etc 'static components' to receive it from any point of a Flutter app. Note, that Vader not listening on all dependencies like a Provider. And it's use case just to define and pass static 'business logic' dependencies. It's only listening on DiContainer and rebuilds ModuleInjector's subtree only if the parent DiContainer changes, because it passes through app via a Provider.

Basic Usage #

A ModuleInjector is just a Widget wrapper around a DiContainer vader class. It is an abstract, so you need to subclass it to override a configure(DiContainer) build(BuildContext) methods. The first one is just defines dependencies via DiContainer (see Vader) for the subtree building from build(BuildContext) method.

A DependentWidget is just a receiver of your dependencies, defined in the ModuleInjector. So in the builder parameter you need to pass a factory function that builds a child widget with this dependencies. It's very similar to Provider's Consumer widget but without any context or other widget parameters to avoid boilerplate code with _ and __ arguments.

A NavigatorModule widget adds convenient and flexible way to create a nested Navigator with it's own context. You can combine it with ModuleInjector and get independent module with it's own dependencies. Note that if you want to use a Hero you in the nested Navigator - you need to add a HeroController to observers parameter in Navigator or NavigatorModule.

Tired of a boring builder and factory writings for Widgets? Look at InjectableWidget<T>. It just resolves and builds as child configured earlier Widget of type T

Example #

import 'package:flutter/material.dart';
import 'package:vader_di/vader.dart';
import 'package:vader_flutter/vader_flutter.dart';

void main() {
  runApp(
    ModuleValueInjector<String>(
      valueToResolve: 'Hello world',
      child: DependentWidget<String>(
        builder: (val) => Text(val.toString()),
      ),
    )
  );
}

class TestableWidget<T extends Object> extends StatelessWidget {
  final T valueToResolve;

  const TestableWidget({
    Key key,
    this.valueToResolve
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ModuleValueInjector(
      valueToResolve: valueToResolve,
      child: DependentWidget<T>(
        builder: (val) => Text(val.toString()),
      ),
    );
  }
}

class ModuleValueInjector<T> extends ModuleInjector {
  final Widget child;
  final T valueToResolve;

  ModuleValueInjector({
    Key key,
    @required this.child,
    @required this.valueToResolve
  }) : super(key: key);

  @override
  void configure(DiContainer container) {
    container.bind<T>().toValue(valueToResolve);
  }
}

[1.1.0] 2020/03/03 #

Updated to the vader_di 1.1.0. Added CustomFactories in its core. See vader_di for more details

[1.0.0] 2020/02/20 #

Updated to the vader_di 1.0.0. Note! Breaking changes. See vader_di for more details

[0.6.0] 2020/01/14 #

Updated to the vader_di 0.3.1

[0.5.0] 2020/01/09 #

Updated to the flutter 1.12.13+hotfix5 and to the dart 2.7.0 Fixed tests

[0.4.0] 2019/12/30 #

Fixed bug in ModuleInjector. Changed ModuleInjector's API

[0.3.1] 2019/12/25 #

Added InjectableWidget. Updated vader_di to 0.2.1

[0.2.2] 2019/12/25 #

Added NavigatorModule. Fixed some compile issues when working with Flutter

[0.1.0] 2019/12/12 #

First release

example/example.dart

import 'package:flutter/material.dart';
import 'package:vader_di/vader.dart';
import 'package:vader_flutter/vader_flutter.dart';

void main() {
  runApp(
    ModuleValueInjector<String>(
      valueToResolve: 'Hello world',
      child: DependentWidget<String>(
        builder: (val) => Text(val.toString()),
      ),
    )
  );
}

class TestableWidget<T extends Object> extends StatelessWidget {
  final T valueToResolve;

  const TestableWidget({
    Key key,
    this.valueToResolve
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ModuleValueInjector(
      valueToResolve: valueToResolve,
      child: DependentWidget<T>(
        builder: (val) => Text(val.toString()),
      ),
    );
  }
}

class ModuleValueInjector<T> extends ModuleInjector {
  final Widget child;
  final T valueToResolve;

  ModuleValueInjector({
    Key key,
    @required this.child,
    @required this.valueToResolve
  }) : super(key: key);

  @override
  void configure(DiContainer container) {
    container.bind<T>().toValue(valueToResolve);
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  vader_flutter: ^1.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:vader_flutter/vader_flutter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
28
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • vader_flutter that is a package requiring null.

Health suggestions

Format lib/src/dependent_widget.dart.

Run flutter format to format lib/src/dependent_widget.dart.

Format lib/src/injectable_widget.dart.

Run flutter format to format lib/src/injectable_widget.dart.

Format lib/src/navigator_module.dart.

Run flutter format to format lib/src/navigator_module.dart.

Format lib/vader_flutter.dart.

Run flutter format to format lib/vader_flutter.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
meta ^1.1.8 1.1.8 1.2.2
provider ^4.0.4 4.3.1
vader_di ^1.1.0 1.1.0
Transitive dependencies
async 2.4.2
boolean_selector 2.0.0
charcode 1.1.3
collection 1.14.12 1.14.13
disposable_utils 1.2.0
matcher 0.12.8
nested 0.0.4
path 1.7.0
sky_engine 0.0.99
source_span 1.7.0
stack_trace 1.9.5
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
test_api 0.2.17
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test
mockito ^4.1.1 4.1.1