Language: English | 中文简体

Packages: observable_builder

observable_builder

  • Lightweight Flutter state management library with support for local refreshes and state caching.

Use

//*****************************************************
// use the example:
//*****************************************************
class CounterState {
  int counter = 0;
}

var observableCounter = Observable(CounterState());

class TestWidget extends StatefulWidget {
  @override
  _TestWidgetState createState() => _TestWidgetState();
}

class _TestWidgetState extends State<TestWidget> {
  @override
  void initState() {
    observableCounter.listen((state) {
      print(state.counter);
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ObservableBuilder<CounterState>(
          create: observableCounter,
          memo: (state) => [state.counter],
          builder: (context, state) {
            return Text('${state.counter}');
          },
        ),
        IconButton(
          onPressed: () => observableCounter.notify((state) => state.counter++),
          icon: Icon(Icons.add),
        )
      ],
    );
  }
}

support the cache

//NoCache
class NoCacheCounterState {
  int counter = 0;
}
//use
var observableCounter = Observable(NoCacheCounterState());

//Cache
class CacheCounterState {
  int counter;

  CacheCounterState({this.counter = 0});

  static CacheCounterState fromJson(dynamic json) {
    return CacheCounterState(
      counter: json["counter"],
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['counter'] = this.counter;
    return data;
  }
}

//use
var observableCounter = Observable(CacheCounterState(),
    cacheKey: 'CounterState_CacheKey', decoder: CacheCounterState.fromJson);

related reference libraries and projects

  • xanno (use annotations to simplify development @GApi @GAssets @GColor @GEntity @GFormatter @GHttp @GIconFont @GProject @GRoute @GSize.)

Libraries

observable