Skip to content

Instantly share code, notes, and snippets.

@felixblaschke
Last active February 14, 2021 09:00
Show Gist options
  • Save felixblaschke/5625179cd812734b9e07c2e7b119ddeb to your computer and use it in GitHub Desktop.
Save felixblaschke/5625179cd812734b9e07c2e7b119ddeb to your computer and use it in GitHub Desktop.
riverpod5.dart
var name = StateProvider((_) => "Peter");
var age = StateProvider((_) => 32);
// Combines "name" and "age" to a String
var greeting = Provider<String>((scope) {
return "Hi! My name is ${scope.watch(name).state} and "
"I am ${scope.watch(age).state} years old.";
});
// Refines the "greeting" String to a Widget
var greetWidget = Provider<Widget>((scope) {
// You don't need to access the ".state" property
// because "greeting" is no StateProvider
return Text(scope.watch(greeting));
});
// Use everything in Flutter
class SomeWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer(
builder: (context, watch, _) {
// You don't need to access the ".state" property
// because "greetWidget" is no StateProvider
return Center(child: watch(greetWidget));
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment