Skip to content

Instantly share code, notes, and snippets.

@rrousselGit
Created August 1, 2019 10:19
Show Gist options
  • Select an option

  • Save rrousselGit/fc63b69b88d1716ffbcb1038b570e1a0 to your computer and use it in GitHub Desktop.

Select an option

Save rrousselGit/fc63b69b88d1716ffbcb1038b570e1a0 to your computer and use it in GitHub Desktop.
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