djinn_flutter 0.2.0 copy "djinn_flutter: ^0.2.0" to clipboard
djinn_flutter: ^0.2.0 copied to clipboard

outdated

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 #

container.dart

class _Container {
  Djinn container;

  _Container() {
    container = Djinn();
    container.register(() => Counter()); // singleton => same instance on every 'get' call
    // or
    container.register(() => Counter(), true); // scoped = true => new instance on every 'get' call 
    container.init();
  }
}

final container = _Container().container;

counter.dart

class Counter extends DjinnService {
  var state = {
    'count': 0
  };

  increment() {
    patch({
      'count': state['count'] + 1
    });
  }

  decrement() {
    patch({
      'count': state['count'] - 1
    });
  }
}

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 = container.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 to changes #

You can use the default way listen and dispose.

Or if you want more control:

var unsubscribe = counter.subscribe((Map<String, StateUpdate> update) {
});

StateUpdate is a data class that contains the current and previous values.

The update is a Map that contains all property updates with its corresponding values.

The unsubscribe is a function that when called will remove the given subscribe function of the listeners list.

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 = container.get();
  }
  
  doSomething() {
    service.printOk();
  }
}
0
likes
0
pub points
0%
popularity

Publisher

unverified uploader

Application state management, using class inheritance to create services.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on djinn_flutter