Created with <3 with dartpad.dev.
Created
November 25, 2023 21:50
-
-
Save knaeckeKami/a96f0581fac6eba24c40bcf2ec9cd8ad 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); | |
// some future provider that holds some value | |
final sourceProvider = FutureProvider((ref) async { | |
await Future.delayed(Duration(seconds: 1)); | |
return Random().nextInt(42); | |
}); | |
// a provider that may extract some value from sourceProvider | |
final dependendProvider = FutureProvider((ref) { | |
return ref.watch(sourceProvider.selectAsync((data) => data)); | |
}); | |
// used whenData to double the value | |
// has 1 frame loading state when sourceProvider is invaidated | |
final doubleProvider = Provider((ref) { | |
return ref.watch(dependendProvider).whenData((data) => data * 2); | |
}); | |
// uses manual checking for availble data to double the value | |
// -> works synchously without 1 frame loading states | |
// if sourceProvider is invalidated | |
final doubleWithManualCheckingProvider = Provider((ref) { | |
final sourceVal = ref.watch(dependendProvider); | |
return switch (sourceVal) { | |
AsyncValue(hasValue: true, :final value?) => AsyncData(value * 2), | |
AsyncError(:final error, :final stackTrace) => | |
AsyncError(error, stackTrace), | |
AsyncLoading() || _ => const AsyncLoading(), | |
}; | |
}); | |
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); | |
final doubleWithManualCheckingValue = | |
ref.watch(doubleWithManualCheckingProvider); | |
print("source: " + sourceVal.toString()); | |
print("dep: " + dependendProviderValue.toString()); | |
print("double: " + doubleProviderValue.toString()); | |
print("double manual: " + doubleWithManualCheckingValue.toString()); | |
print("---"); | |
return Column( | |
children: [ | |
Text(sourceVal.toString()), | |
Text(dependendProviderValue.toString()), | |
Text(doubleProviderValue.toString()), | |
Text(doubleWithManualCheckingValue.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