Created
September 20, 2024 17:57
-
-
Save chillbrodev/7071c20492cb697fad157c22ead4b4ea to your computer and use it in GitHub Desktop.
Demo for Riverpod List Reference
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
import 'package:flutter/material.dart'; | |
import 'dart:async'; | |
import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
final testListProvider = FutureProvider<List<int>>((ref) async { | |
await Future.delayed(const Duration(milliseconds: 200)); | |
return [1, 2, 3]; | |
}); | |
class TestNotifier extends AsyncNotifier<List<int>> { | |
@override | |
FutureOr<List<int>> build() async { | |
final data = await ref.watch(testListProvider.future); | |
final List<int> copy = List.from(data); | |
print("TestNotifier - Data: $copy"); | |
print("TestNotifier - HashCode: ${copy.hashCode}"); | |
copy.removeAt(0); | |
print("TestNotifier - Data After Remove: $copy"); | |
print("TestNotifier - HashCode After Remove: ${copy.hashCode}"); | |
return copy; | |
} | |
} | |
class TestNotifierTwo extends AsyncNotifier<List<int>> { | |
@override | |
FutureOr<List<int>> build() async { | |
final data = await ref.watch(testListProvider.future); | |
print("TestNotifierTwo - Data: $data"); | |
print("TestNotifierTwo - HashCode: ${data.hashCode}"); | |
return data; | |
} | |
} | |
class Combiner extends AsyncNotifier<List<int>> { | |
@override | |
FutureOr<List<int>> build() async { | |
final data = await ref.watch(testProvider.future); | |
final data2 = await ref.watch(testTwoProvider.future); | |
print("Combiner - Data HashCode: ${data.hashCode}"); | |
print("Combiner - Data2 HashCode: ${data2.hashCode}"); | |
return [0]; | |
} | |
} | |
final testProvider = | |
AsyncNotifierProvider<TestNotifier, List<int>>(TestNotifier.new); | |
final testTwoProvider = | |
AsyncNotifierProvider<TestNotifierTwo, List<int>>(TestNotifierTwo.new); | |
final combinerProvider = | |
AsyncNotifierProvider<Combiner, List<int>>(Combiner.new); | |
void main() { | |
runApp(const ProviderScope(child: MyApp())); | |
} | |
class MyApp extends ConsumerWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context, WidgetRef ref) { | |
ref.listen(combinerProvider, (prev, curr) => print(curr)); | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith( | |
useMaterial3: true, | |
iconButtonTheme: IconButtonThemeData( | |
style: IconButton.styleFrom( | |
hoverColor: Colors.transparent, | |
focusColor: Colors.transparent, | |
highlightColor: Colors.transparent, | |
), | |
), | |
), | |
debugShowCheckedModeBanner: false, | |
home: const Scaffold( | |
body: Center( | |
child: Text('Some text'), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment