Created with <3 with dartpad.dev.
Created
November 25, 2023 21:39
-
-
Save knaeckeKami/6f8bb7e6e455c051b79067cb276f37c1 to your computer and use it in GitHub Desktop.
iridescent-tundra-1306
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
import 'dart:math'; | |
import 'package:flutter/material.dart'; | |
import 'package:hooks_riverpod/hooks_riverpod.dart'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
final sourceProvider = FutureProvider((ref) async { | |
await Future.delayed(Duration(seconds: 1)); | |
return Random().nextInt(42); | |
}); | |
final dependendProvider = FutureProvider((ref) { | |
return ref.watch(sourceProvider.selectAsync((data) => data)); | |
}); | |
final doubleProvider = Provider((ref) { | |
return ref.watch(dependendProvider).whenData((data) => data * 2); | |
}); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return ProviderScope( | |
child: MaterialApp( | |
theme: ThemeData.dark().copyWith( | |
scaffoldBackgroundColor: darkBlue, | |
), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: MyWidget(), | |
), | |
), | |
), | |
); | |
} | |
} | |
class MyWidget extends ConsumerWidget { | |
@override | |
Widget build(BuildContext context, WidgetRef ref) { | |
final sourceVal = ref.watch(sourceProvider); | |
final dependendProviderValue = ref.watch(dependendProvider); | |
final doubleProviderValue = ref.watch(doubleProvider); | |
print("source: " + sourceVal.toString()); | |
print("dep: " + dependendProviderValue.toString()); | |
print("double: " + doubleProviderValue.toString()); | |
print("---"); | |
return Column( | |
children: [ | |
Text(sourceVal.toString()), | |
Text(dependendProviderValue.toString()), | |
Text(doubleProviderValue.toString()), | |
TextButton( | |
onPressed: () { | |
print("invalidate"); | |
ref.invalidate(sourceProvider); | |
}, | |
child: Text("invalidate")), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment