djinn_flutter 0.3.0

Flutter Android iOS web

Application state management, using class inheritance to create services.

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
likes
80
pub points
0%
popularity

Application state management, using class inheritance to create services.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

gilmarsquinelato@gmail.com

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on djinn_flutter