Last active
February 14, 2021 09:00
-
-
Save felixblaschke/5625179cd812734b9e07c2e7b119ddeb to your computer and use it in GitHub Desktop.
riverpod5.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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