Last active
December 8, 2022 13:12
-
-
Save biniama/4ba5140f525f8717bef367872484611b to your computer and use it in GitHub Desktop.
AsyncNotifier and AsyncNotifierProvider Example for Flutter
This file contains 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
//Add the following in `pubspec.yaml` | |
dependencies: | |
flutter_riverpod: ^2.1.1 | |
//If you want to run the example, you also need to install `shared_preferences: ^2.0.13` | |
//More documentation can be found at //https://codewithandrea.com/articles/flutter-riverpod-async-notifier/ | |
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
import '../shared_preferences_utils.dart'; | |
void main() async { | |
// Adding ProviderScope enables Riverpod for the entire project | |
runApp(ProviderScope(child: MyApp())); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'AsyncNotifer Example', | |
debugShowCheckedModeBanner: false, | |
home: FavScreen() | |
); | |
} | |
} | |
final favProvider = AsyncNotifierProvider<FavNotifier, List<String>>(FavNotifier.new); | |
class FavNotifier extends AsyncNotifier<List<String>> { | |
@override | |
FutureOr<List<String>> build() { | |
return loadFavs(); | |
} | |
toggle(taskId) async { | |
update((_) async { | |
state = const AsyncLoading(); | |
return await updateFavs(taskId); | |
}); | |
} | |
} | |
class FavScreen extends ConsumerWidget { | |
final List<Map<String, String>> data = [ | |
{"id": '1', 'title': '1'}, | |
{"id": '2', 'title': '2'}, | |
{'id': '3', 'title': '3'}, | |
{'id': '4', 'title': '4'} | |
]; | |
@override | |
Widget build(BuildContext context, WidgetRef ref) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('State Management with RiverPod!'), | |
), | |
body: Column( | |
children: data.map((element) => FavItem(fav: element)).toList(), | |
), | |
); | |
} | |
} | |
class FavItem extends ConsumerWidget { | |
final Map<String, String> fav; | |
const FavItem({Key? key, required this.fav}) : super(key: key); | |
@override | |
Widget build(BuildContext context, WidgetRef ref) { | |
// watch the AsyncProvider | |
final favAsync = ref.watch(favProvider); | |
// use pattern matching to map the state to the UI | |
return favAsync.when( | |
loading: () => const CircularProgressIndicator(), | |
error: (err, stack) => Text('Error: $err'), | |
data: (result) { | |
return Padding( | |
padding: const EdgeInsets.all(18.0), | |
child: Row( | |
children: [ | |
Checkbox( | |
onChanged: (newValue) => | |
ref.read(favProvider.notifier).toggle(fav['id']!), | |
value: result.contains(fav['id']), | |
), | |
Text(fav['title']!), | |
], | |
), | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment