The code below takes user input, and prints it in the upper case after a delay of 1s.
Minimal reproducible code:
class FooPage extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final asyncValue = ref.watch(resultProvider);
print('loading: ${asyncValue.isLoading}');
return Scaffold(
body: Column(
children: [
TextField(onChanged: (s) => ref.read(queryProvider.notifier).state = s),
asyncValue.when(
data: Text.new,
error: (e, s) => Text('Error = $e'),
loading: () => Text('Loading...'),
),
],
),
);
}
}
final stringProvider = FutureProvider.family<String, String>((ref, query) async {
await Future.delayed(Duration(seconds: 1));
return query.toUpperCase();
});
final queryProvider = StateProvider<String>((ref) => '');
final resultProvider = FutureProvider<String>((ref) async {
final query = ref.watch(queryProvider);
return ref.watch(stringProvider(query).future);
});
After running the code,
- Enter any text (say
a
) and wait for the output (the upper case) - Enter another text (say
b
), and wait for the output (the upper case) - Press the backspace (i.e. delete the character
b
) and now the console will printloading: true
and because of this the loading widget builds for a fraction of seconds. This causes a very poor UX.
This issue is happening on the latest 2.0.2
version. So, how can I get the previous value so I can consistently show data
once a data is fetched?
Update:
This is how I'm using the git:
dependencies:
flutter:
sdk: flutter
flutter_riverpod:
git:
url: https://github.com/rrousselGit/riverpod/tree/master/packages/flutter_riverpod
ref: master