stateprovider 0.5.1 stateprovider: ^0.5.1 copied to clipboard
A lightweight StateProvider for handling global data. This Plugin wraps an InheritedWidget into a StreamBuilder for updating the UI if any changes appears in the InheritedWidget.
StateProvider for Flutter #
A lightweight StateProvider for handling global data. This Plugin wraps an InheritedWidget into a StreamBuilder for updating the UI if any changes appears in the InheritedWidget.
Getting Started #
1. Import stateprovider
import 'package:stateprovider/stateprovider.dart';
2. Create a Model which extends the Store class. Add your custom attributes and functions. Call notify() in every function where you want to update the state.
class MyModel extends Store<MyModel> {
String name = "";
int age = 0;
//stores only the new data but doesn't update the ui
void setName(String name) {
this.name = name;
}
//updates the ui because of notify()
void setAge(int age) {
this.index = index;
notify();
}
}
3. Add the StatefulProvider widget in the SECOND position of your tree under MaterialApp or CupertinoApp. Add your custom Model to the StatefulProvider.
void main() => runApp(
MaterialApp(
home: StatefulProvider(
store: MyModel(),
child: PageOne(),
),
)
);
4. Now inside your widget you can use your data with the StateProvider class:
StateProvider.of(context).getState<MyModel>().setAge(34)
Additional #
if you want to update the state manually use:
StateProvider.of(context).getState<MyModel>().notify()