Created
August 1, 2019 10:19
-
-
Save rrousselGit/fc63b69b88d1716ffbcb1038b570e1a0 to your computer and use it in GitHub Desktop.
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
| class UserDetails { | |
| UserDetails(this.id); | |
| final int id; | |
| } | |
| class User extends StatefulWidget { | |
| const User({Key key, this.id, this.builder, this.child}) : super(key: key); | |
| final int id; | |
| final ValueWidgetBuilder<AsyncSnapshot<UserDetails>> builder; | |
| final Widget child; | |
| @override | |
| _UserState createState() => _UserState(); | |
| } | |
| class _UserState extends State<User> { | |
| Future<UserDetails> future; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| future = fetchUser(widget.id); | |
| } | |
| @override | |
| void didUpdateWidget(User oldWidget) { | |
| super.didUpdateWidget(oldWidget); | |
| if (widget.id != oldWidget.id) { | |
| future = fetchUser(widget.id); | |
| } | |
| } | |
| Future<UserDetails> fetchUser(int id) => Future.value(UserDetails(id)); | |
| @override | |
| Widget build(BuildContext context) { | |
| return FutureBuilder<UserDetails>( | |
| future: future, | |
| builder: (context, snapshot) { | |
| return widget.builder(context, snapshot, widget.child); | |
| }, | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment