honeycomb_flutter 0.8.0
honeycomb_flutter: ^0.8.0 copied to clipboard
honeycomb for Flutter
Honeycomb for Flutter
For more information about DI itself see honeycomb
Navigation #
Features #
- Inject your providers via context
- Share scoped providers across multiple routes
Getting started #
final counterProvider = Provider((_) => ValueNotifier<int>(0));
void main() {
runApp(
// For widgets to be able to read providers, we need to wrap the entire
// application in a "ProviderScope" widget.
ProviderScope(
child: App(),
)
);
}
class App extends StatelessWidget {
Widget build(BuildContext) {
return Column(
children: [
ValueListenableBuilder(
valueListenable: counterProvider.of(context, listen: true),
builder: (_, value, __) => Text("Count: $value"),
),
OutlinedButton(
onPressed: () {
counterProvider.of(context).value++;
},
child: Text("Increment"),
)
]
);
}
}
Usage #
How to read a provider? #
If you're reading a provider inside build method, use:
myProvider.of(context, listen: true);
Remark: You wouldn't need listen: true very often.
You should either use honeybloc package or write your own wrapper, depending on state management you use.
In most cases (lifecycle method, callbacks) you should use it like:
myProvider.of(context);
Scoping #
You could introduce scopes to create and dispose your providers together with its widget trees.
final counterProvider = Provider(
(_) => ValueNotifier<int>(0),
dispose: (vn) {
vn.dispose();
print("Dispose");
},
);
class App extends StatelessWidget {
Widget build(BuildContext) {
return Column(
children: [
ValueListenableBuilder(
valueListenable: counterProvider.of(context, listen: true),
builder: (_, value, __) => Text("Count: $value"),
),
OutlinedButton(
onPressed: () {
counterProvider.of(context).value++;
},
child: Text("Increment"),
)
]
);
}
}
class SecondPage extends StatelessWidget {
Widget build(BuildContext context) {
return ProviderScope(
overrides: [counterProvider.scope()],
child: Builder(
builder: (context) => Column(
children: [
ValueListenableBuilder(
valueListenable: counterProvider.of(
context,
listen: true,
),
builder: (_, value, __) => Text("Count: $value"),
),
OutlinedButton(
onPressed: () {
counterProvider.of(context).value++;
},
child: Text("Increment"),
)
]
)
)
);
}
}
Now, everytime you pop and push SecondPage its counter will be recreated. Also you will see "Dispose" in console.
Global counter will remain unctouched.
To test it yourself see scoping example