observable_builder 0.0.3
observable_builder: ^0.0.3 copied to clipboard

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

Language: English | 中文简体

Packages: observable_builder

observable_builder #

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

Use #

support 1.obs ''.obs [].obs {}.obs, nocache #

//*****************************************************
// use the example:
//*****************************************************
class TestWidget extends StatefulWidget {
  @override
  _TestWidgetState createState() => _TestWidgetState();
}

class _TestWidgetState extends State<TestWidget> {
  final countObs = 0.obs;

  @override
  void initState() {
    observableCounter.listen((state) {
      print(state.current);
    });
    super.initState();
  }

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

support the cache #

//*****************************************************
// 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 nocache #

//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.)
1
likes
110
pub points
17%
popularity

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

Homepage
Repository

Documentation

API reference

License

Apache-2.0 (LICENSE)

Dependencies

flutter, isolate, shared_preferences

More

Packages that depend on observable_builder