djinn_flutter 0.3.0

djinn_flutter #

Djinn-state implementation for flutter.

The djinn-state is an application state management, using class inheritance to create services.

The services are classes with business logic, side-effects and state.

Getting Started #

main.dart

void main() {
  Djinn().register(() => CounterService()); // singleton => same instance on every 'get' call
  // or
  Djinn().register(() => CounterService(), true); // scoped = true => new instance on every 'get' call
  Djinn().init();

  runApp(MyApp());
}

counter.dart

import 'package:djinn_flutter/djinn_flutter.dart';

class CounterState {
  int count;

  CounterState({this.count});
}

class CounterService extends DjinnService<CounterState> {
  var state = CounterState(count: 0);

  increment() {
    state.count++;
    updateState();
  }

  decrement() {
    state.count--;
    updateState();
  }
}

counter_widget.dart

class Counter extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _CounterState createState() => _CounterState();
}

class _CounterState extends State<Counter> {
  final Counter counter = Djinn().get(); // <= Look this

  @override
  void initState() {
    super.initState();
    counter.listen(this); // <= Look this
  }

  @override
  void dispose() {
    counter.dispose(this); // <= Look this
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '${counter.state.count}', // <= Look this
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: counter.increment, // <= Look this
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Listening for changes #

The listen and dispose are functions to subscribe and unsubscribe for the service changes.

The updateState function called inside the services tell to DjinnService to propagate its state changes to the listeners.

Important #

The init function is important, and must be called after all services was registered.

That function is responsible to create instances of the singleton services (scoped = false) and call it's init methods.

Communication between services #

Just call the container.get() function inside the init function.

class ServiceA extends DjinnService {
  void printOk() {
    print('ok');
  }
}

class ServiceB extends DjinnService {
  ServiceA service;
  init() {
    service = Djinn().get();
  }
  
  doSomething() {
    service.printOk();
  }
}

[0.3.0]

  • Changed DjinnService to have a class as state.
  • Changed patch to updateState and not receive arguments.
  • Removed the update information from the callbacks.

[0.2.1]

  • Changed Djinn to be a singleton.

[0.1.0]

  • Initial release.

example/lib/main.dart

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

import 'counter_service.dart';

void main() {
  Djinn().register(() => CounterService());
  Djinn().init();

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Counter'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final CounterService counter = Djinn().get();

  @override
  void initState() {
    super.initState();
    counter.listen(this);
  }

  @override
  void dispose() {
    counter.dispose(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '${counter.state.count}',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: counter.increment,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  djinn_flutter: ^0.3.0

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:djinn_flutter/djinn_flutter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
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]
50
Learn more about scoring.

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

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/generated/i18n.dart.

Run flutter format to format lib/generated/i18n.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test

Admin