stark 2.0.2
stark: ^2.0.2 copied to clipboard

Stark - a pragmatic lightweight dependency injection framework for Dart

stark #

What is Stark? #

A pragmatic lightweight dependency injection framework for Dart developers.

Some info #

This implementation does not rely on the dart reflection apis (mirrors) and favours a simple factory based approach. This increases the performance and simplicity of this implementation.

Any help is appreciated! Comment, suggestions, issues, PR's!

Getting Started #

In your flutter or dart project add the dependency:

dependencies:
  ...
  stark: 2.0.0

Usage example #

Import stark

import 'package:stark/stark.dart';

Modules definition #

import 'package:stark/stark.dart';

final appModule = {
    single((i) => Api()), 
    single<Repository>((i) => MyRepository(i.get())),
    factory((i) => UseCase(i.get())), 
    factoryWithParams((i, p) => ViewModel(i.get(), p["dynamicParam"])),
};

Initialize Stark #


//You can pass a list with many modules
Stark.init([
  appModule,
  domainModule,
  presentationModule
]);

Getting a inject instance #

class LoginScreen extends StatefulWidget{
  @override
  State<StatefulWidget> createState()  => LoginScreenState();
}

class LoginScreenState extends State<LoginScreen>{

  final _loginViewModel = Stark.get<LoginViewModel>();

  @override
  Widget build(BuildContext context) {
   
    return  Container(
        ...
    );
  }
}

Singleton definition: #

import 'package:stark/stark.dart';

final myModule = {
    single((i) => Api(i.get())), 
};

Single with dynamic param: #

import 'package:stark/stark.dart';

final myModule = {
    singleWithParams((i,p) => Api(p["token"])), 
};

Factory definition #

import 'package:stark/stark.dart';

final myModule = {
    factory((i) => UseCase()), 
};

Factory with dynamic param: #

import 'package:stark/stark.dart';

final myModule = {
    factoryWithParams((i,p) => Api(p["token"])), 
};

Named injections #

import 'package:stark/stark.dart';

final myModule = {
    single((i) => Api(), named: "DEFAULT"), 
    single((i) => Api(), named: "EXTERNAL"), 
};


// to get named injections
Stark.get<Api>(named: "DEFAULT");

Dynamic params #

import 'package:stark/stark.dart';

final myModule = {
    singleWithParams((i,p) => Api(p["token"])), 
    factoryWithParams((i,p) => MyPresenter(p["view"])), 
};


// to get with dynamic params
Stark.get<Api>(params: { "token" : "Bearer asdasdad"})
Stark.get<MyPresenter>(params: { "view" : this})

Scoped injections #

  • You can mix your class with Stark component to associate the injection life time to your widget:
class _MyHomePageState extends State<MyHomePage> with StarkComponent {
  ViewModel _viewModel;

  @override
  void initState() {
    super.initState();

    //View model instance will destroyed when this widget state call dispose.
    _viewModel = get<ViewModel>(
        params: <String, dynamic>{'name': 'Custom dynamic param'});
  }
...

Disposable Interface #

Stark provides a Disposable interface, when a injection that implements this interface is will be dispose the method dispose is called, is very nice to viewModels for example:


class LoginViewModel implements Disposable {
    @override
    dispose(){
      //this method is called when the LoginViewModel is diposed, use to dispose your RX Subjects or Streams
    }
}

License #

Copyright 2020 The Stark Authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

3
likes
120
pub points
47%
popularity

Stark - a pragmatic lightweight dependency injection framework for Dart

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

diefferson.sts@gmail.com

License

Apache 2.0 (LICENSE)

Dependencies

analyzer, flutter

More

Packages that depend on stark