Skip to content

Commit

Permalink
Remove AsyncValue.when in references in favour of using switch-case
Browse files Browse the repository at this point in the history
  • Loading branch information
rrousselGit committed Aug 24, 2023
1 parent 9d4edfd commit 0549544
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 76 deletions.
10 changes: 5 additions & 5 deletions website/docs/concepts/reading.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,11 @@ When reading this `userProvider`, you can:
Widget build(BuildContext context, WidgetRef ref) {
AsyncValue<User> 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(),
};
}
```

Expand Down
20 changes: 9 additions & 11 deletions website/docs/cookbooks/search_as_we_type.mdx
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -113,24 +113,22 @@ 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(
question.toString(),
),
);
},
);
},
),
);,
AsyncError(:final error) => Center(child: Text('Error $error')),
_ => const Center(child: CircularProgressIndicator()),
},
),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
},
));
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
},
));
);
}
}
}
12 changes: 5 additions & 7 deletions website/docs/providers/future_provider/config_consumer/raw.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import '../config_provider/raw.dart';
Widget build(BuildContext context, WidgetRef ref) {
AsyncValue<Configuration> 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(),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
}

0 comments on commit 0549544

Please sign in to comment.