djinn_flutter 0.2.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new56

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(() => Counter()); // singleton => same instance on every 'get' call
  // or
  Djinn().register(() => Counter(), true); // scoped = true => new instance on every 'get' call
  Djinn().init();

  runApp(MyApp());
}

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 = 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 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 = Djinn().get();
  }
  
  doSomething() {
    service.printOk();
  }
}

[0.2.1]

  • Changed Djinn to be a singleton

[0.1.0]

  • Initial release.

example/main.dart

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

import 'counter.dart';

void main() {
  Djinn().register(() => Counter());
  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 Counter 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.2.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages 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';
  
Version Uploaded Documentation Archive
0.2.1 Feb 21, 2019 Go to the documentation of djinn_flutter 0.2.1 Download djinn_flutter 0.2.1 archive
0.2.0 Feb 21, 2019 Go to the documentation of djinn_flutter 0.2.0 Download djinn_flutter 0.2.0 archive
0.1.1 Feb 21, 2019 Go to the documentation of djinn_flutter 0.1.1 Download djinn_flutter 0.1.1 archive
0.1.0 Feb 21, 2019 Go to the documentation of djinn_flutter 0.1.0 Download djinn_flutter 0.1.0 archive
0.0.1 Feb 21, 2019 Go to the documentation of djinn_flutter 0.0.1 Download djinn_flutter 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
13
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
56
Learn more about scoring.

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

  • Dart: 2.2.0
  • pana: 0.12.14
  • Flutter: 1.3.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health issues and suggestions

Document public APIs. (-1 points)

31 out of 31 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

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