From 0549544a02f03a7785c0b38932bbfc1fb8fe425d Mon Sep 17 00:00:00 2001 From: Remi Rousselet Date: Thu, 24 Aug 2023 11:27:29 +0200 Subject: [PATCH] Remove AsyncValue.when in references in favour of using switch-case --- website/docs/concepts/reading.mdx | 10 +++--- website/docs/cookbooks/search_as_we_type.mdx | 20 +++++------ .../config_consumer/codegen.dart | 13 +++---- .../config_consumer/hooks.dart | 11 +++--- .../config_consumer/hooks_codegen.dart | 17 ++++----- .../future_provider/config_consumer/raw.dart | 12 +++---- .../remote_todos/todos_consumer.dart | 35 +++++++++---------- .../live_stream_chat_consumer.dart | 21 ++++++----- 8 files changed, 63 insertions(+), 76 deletions(-) diff --git a/website/docs/concepts/reading.mdx b/website/docs/concepts/reading.mdx index 31479b298..9e15968f3 100644 --- a/website/docs/concepts/reading.mdx +++ b/website/docs/concepts/reading.mdx @@ -323,11 +323,11 @@ When reading this `userProvider`, you can: Widget build(BuildContext context, WidgetRef ref) { AsyncValue user = ref.watch(userProvider); - return user.when( - loading: () => const CircularProgressIndicator(), - error: (error, stack) => const Text('Oops'), - data: (user) => Text(user.name), - ); + return switch (user) { + AsyncData(:final value) => Text(value.name), + AsyncError(:final error) => const Text('Oops $error'), + _ => const CircularProgressIndicator(), + }; } ``` diff --git a/website/docs/cookbooks/search_as_we_type.mdx b/website/docs/cookbooks/search_as_we_type.mdx index 3e1d53e62..a6525c632 100644 --- a/website/docs/cookbooks/search_as_we_type.mdx +++ b/website/docs/cookbooks/search_as_we_type.mdx @@ -1,8 +1,8 @@ --- title: Search as we type --- -A real world example could be to use `FutureProvider` to implement a searchbar. +A real world example could be to use `FutureProvider` to implement a searchbar. ## Usage example: "Search as we type" searchbar @@ -113,14 +113,11 @@ class MyHomePage extends ConsumerWidget { ref.read(searchFieldProvider.notifier).state = value, ), Expanded( - child: questions.when( - loading: () => const Center(child: CircularProgressIndicator()), - error: (error, stack) => Center(child: Text('Error $error')), - data: (questions) { - return ListView.builder( - itemCount: questions.length, + child: switch (questions) { + AsyncData(:final value) => ListView.builder( + itemCount: value.length, itemBuilder: (context, index) { - final question = questions[index]; + final question = value[index]; return ListTile( title: Text( @@ -128,9 +125,10 @@ class MyHomePage extends ConsumerWidget { ), ); }, - ); - }, - ), + );, + AsyncError(:final error) => Center(child: Text('Error $error')), + _ => const Center(child: CircularProgressIndicator()), + }, ), ], ), diff --git a/website/docs/providers/future_provider/config_consumer/codegen.dart b/website/docs/providers/future_provider/config_consumer/codegen.dart index 7f8e854f2..a96f35e72 100644 --- a/website/docs/providers/future_provider/config_consumer/codegen.dart +++ b/website/docs/providers/future_provider/config_consumer/codegen.dart @@ -3,17 +3,14 @@ import 'package:hooks_riverpod/hooks_riverpod.dart'; import '../config_provider/codegen.dart'; - /* SNIPPET START */ Widget build(BuildContext context, WidgetRef ref) { final config = ref.watch(fetchConfigurationProvider); - return config.when( - loading: () => const CircularProgressIndicator(), - error: (err, stack) => Text('Error: $err'), - data: (config) { - return Text(config.host); - }, - ); + return switch (config) { + AsyncError(:final error) => Text('Error: $error'), + AsyncData(:final value) => Text(value.host), + _ => const CircularProgressIndicator(), + }; } diff --git a/website/docs/providers/future_provider/config_consumer/hooks.dart b/website/docs/providers/future_provider/config_consumer/hooks.dart index 3f9e2231c..44d9b4df9 100644 --- a/website/docs/providers/future_provider/config_consumer/hooks.dart +++ b/website/docs/providers/future_provider/config_consumer/hooks.dart @@ -11,12 +11,11 @@ class MyConfiguration extends HookConsumerWidget { Widget build(BuildContext context, WidgetRef ref) { final config = ref.watch(configProvider); return Scaffold( - body: config.when( - loading: () => const Center(child: CircularProgressIndicator()), - error: (err, stack) => Center(child: Text('Error: $err')), - data: (config) { - return Center(child: Text(config.host)); + body: switch (config) { + AsyncError(:final error) => Center(child: Text('Error: $error')), + AsyncData(:final value) => Center(child: Text(value.host)), + _ => const Center(child: CircularProgressIndicator()), }, - )); + ); } } diff --git a/website/docs/providers/future_provider/config_consumer/hooks_codegen.dart b/website/docs/providers/future_provider/config_consumer/hooks_codegen.dart index ece077f72..ade0628fd 100644 --- a/website/docs/providers/future_provider/config_consumer/hooks_codegen.dart +++ b/website/docs/providers/future_provider/config_consumer/hooks_codegen.dart @@ -3,23 +3,20 @@ import 'package:hooks_riverpod/hooks_riverpod.dart'; import '../config_provider/codegen.dart'; - - /* SNIPPET START */ -class MyConfiguration extends HookConsumerWidget { +class MyConfiguration extends HookConsumerWidget { const MyConfiguration({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final config = ref.watch(fetchConfigurationProvider); return Scaffold( - body: config.when( - loading: () => const Center(child: CircularProgressIndicator()), - error: (err, stack) => Center(child: Text('Error: $err')), - data: (config) { - return Center(child: Text(config.host)); + body: switch (config) { + AsyncError(:final error) => Center(child: Text('Error: $error')), + AsyncData(:final value) => Center(child: Text(value.host)), + _ => const Center(child: CircularProgressIndicator()), }, - )); + ); } -} \ No newline at end of file +} diff --git a/website/docs/providers/future_provider/config_consumer/raw.dart b/website/docs/providers/future_provider/config_consumer/raw.dart index 4176a3866..9d39f3bcd 100644 --- a/website/docs/providers/future_provider/config_consumer/raw.dart +++ b/website/docs/providers/future_provider/config_consumer/raw.dart @@ -10,11 +10,9 @@ import '../config_provider/raw.dart'; Widget build(BuildContext context, WidgetRef ref) { AsyncValue config = ref.watch(configProvider); - return config.when( - loading: () => const CircularProgressIndicator(), - error: (err, stack) => Text('Error: $err'), - data: (config) { - return Text(config.host); - }, - ); + return switch (config) { + AsyncData(:final value) => Text(value.host), + AsyncError(:final error) => Text('Error: $error'), + _ => const CircularProgressIndicator(), + }; } diff --git a/website/docs/providers/notifier_provider/remote_todos/todos_consumer.dart b/website/docs/providers/notifier_provider/remote_todos/todos_consumer.dart index ae829eee4..5d00d05d9 100644 --- a/website/docs/providers/notifier_provider/remote_todos/todos_consumer.dart +++ b/website/docs/providers/notifier_provider/remote_todos/todos_consumer.dart @@ -16,23 +16,22 @@ class TodoListView extends ConsumerWidget { final asyncTodos = ref.watch(asyncTodosProvider); // Let's render the todos in a scrollable list view - return asyncTodos.when( - data: (todos) => ListView( - children: [ - for (final todo in todos) - CheckboxListTile( - value: todo.completed, - // When tapping on the todo, change its completed status - onChanged: (value) => - ref.read(asyncTodosProvider.notifier).toggle(todo.id), - title: Text(todo.description), - ), - ], - ), - loading: () => const Center( - child: CircularProgressIndicator(), - ), - error: (err, stack) => Text('Error: $err'), - ); + return switch (asyncTodos) { + AsyncData(:final value) => ListView( + children: [ + for (final todo in value) + CheckboxListTile( + value: todo.completed, + // When tapping on the todo, change its completed status + onChanged: (value) { + ref.read(asyncTodosProvider.notifier).toggle(todo.id); + }, + title: Text(todo.description), + ), + ], + ), + AsyncError(:final error) => Text('Error: $error'), + _ => const Center(child: CircularProgressIndicator()), + }; } } diff --git a/website/docs/providers/stream_provider/live_stream_chat_consumer.dart b/website/docs/providers/stream_provider/live_stream_chat_consumer.dart index 79c186f7a..383def216 100644 --- a/website/docs/providers/stream_provider/live_stream_chat_consumer.dart +++ b/website/docs/providers/stream_provider/live_stream_chat_consumer.dart @@ -6,21 +6,20 @@ import 'live_stream_chat_provider.dart'; /* SNIPPET START */ Widget build(BuildContext context, WidgetRef ref) { final liveChats = ref.watch(chatProvider); + // Like FutureProvider, it is possible to handle loading/error states using AsyncValue.when - return liveChats.when( - loading: () => const CircularProgressIndicator(), - error: (error, stackTrace) => Text(error.toString()), - data: (messages) { - // Display all the messages in a scrollable list view. - return ListView.builder( + return switch (liveChats) { + // Display all the messages in a scrollable list view. + AsyncData(:final value) => ListView.builder( // Show messages from bottom to top reverse: true, - itemCount: messages.length, + itemCount: value.length, itemBuilder: (context, index) { - final message = messages[index]; + final message = value[index]; return Text(message); }, - ); - }, - ); + ), + AsyncError(:final error) => Text(error.toString()), + _ => const CircularProgressIndicator(), + }; }