Skip to content

Instantly share code, notes, and snippets.

@Nunocky
Created February 23, 2024 03:45
Show Gist options
  • Save Nunocky/262b89dcd3e249351bb930df50a28406 to your computer and use it in GitHub Desktop.
Save Nunocky/262b89dcd3e249351bb930df50a28406 to your computer and use it in GitHub Desktop.
AsyncNotifierの使い方
const sentenceList = {
'Your code transforms ideas into reality.',
'Each bug you fix makes you a stronger developer.',
'Your dedication to learning is inspiring.',
'You turn complex problems into elegant solutions.',
'Your creativity in app design is remarkable.',
'Every line of code you write is a step towards mastery.',
'You have a unique talent for making the complex understandable.',
'Your commitment to quality is evident in your work.',
'You\'re not just coding; you\'re crafting future technologies.',
'Your passion for development lights up the path for others.',
};
class MessageNotifier extends AsyncNotifier<String> {
@override
String build() => '';
Future<void> refresh() async {
final _ = await future;
state = const AsyncValue.loading();
await Future.delayed(const Duration(seconds: 1));
final index = Random().nextInt(sentenceList.length);
state = AsyncValue.data(sentenceList.elementAt(index));
}
}
final _messageProvider = AsyncNotifierProvider<MessageNotifier, String>(() {
return MessageNotifier();
});
@override
Widget build(BuildContext context, WidgetRef ref) {
final message = ref.watch(_messageProvider);
...
message.when(
data: (String text) {
return Text(text);
},
error: (Object error, StackTrace stackTrace) {
return Container();
},
loading: () {
return const CircularProgressIndicator();
},
),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment