Skip to content

zuze-lab/dart-stateful

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A tiny state management library for Dart developers.

Why?

Essentially a functional approach to flutter's State except without the flutter.

Usage

A simple usage example:

import 'package:stateful/stateful.dart';

class Person {
  int age;
  String name;

  Person(this.name,this.age);
}

main() {
  var state = Stateful(Person('Joe Smith',39));
  print(state.getState().name); // prints Joe Smith

  var unsub = state.addListener((state) => print('${state.name} is ${state.age}')); // prints Joe Smith is 39
  state.setState(patch: (person) => person..age = person.age+1); // prints Joe Smith is 40
  unsub(); // unsubscribe
  state.setState(patch: (person) => person..age = person.age+1); // doesn't print
}

Selectors

Stolen from reselect. Selectors allow to you select a piece of the state and call a callback only when the selected part of it has changed.

import 'package:stateful/stateful.dart';

class Person {
  int age;
  String name;

  Person(this.name,this.age);
}

main() {
  var state = Stateful(Person('Joe Smith',39));
  

  var unsub = state.addListener(
    Selector1(
      (state) => state.age,
      (age) => print('The person is $age')
    )
  ); // prints The person is 39
  state.setState(patch: (person) => person..age = person.age+1); // prints The person is 40
  state.setState(patch: (person) => person..name = 'Jane Smith'); // doesn't print
}

Dart doesn't support overloading so you need to call a different Selector function (Selector#) depending on how many dependency functions you have.

Features and bugs

Please file feature requests and bugs at the issue tracker.

About

Super tiny state management package

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages