Skip to content

Instantly share code, notes, and snippets.

@gothedistance
Created November 16, 2021 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gothedistance/9620bba97fa297b4a30f39067a0c2b19 to your computer and use it in GitHub Desktop.
Save gothedistance/9620bba97fa297b4a30f39067a0c2b19 to your computer and use it in GitHub Desktop.
set TextField value with FutureProvider
final sampleState = FutureProvider.autoDispose<HogeState>((ref) async {
return HogeState( userName: "demo");
});
class LoginPage extends ConsumerWidget {
final TextEditingController _usernameController = TextEditingController();
@override
Widget build(BuildContext context, WidgetRef ref) {
AsyncValue<HogeState> vm = ref.watch(sampleState);
return vm.when(
data: (data) {
if (data.userName != null) _usernameController.text = data.userName!;
return Scaffold(
body: TextField(
controller: _usernameController,
);
},
loading: () => const CircularProgressIndicator(),
error: (err, stack) => Text('Error: $err'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment