diff --git a/packages/flutter_riverpod/CHANGELOG.md b/packages/flutter_riverpod/CHANGELOG.md index 972634d9c..645814fc6 100644 --- a/packages/flutter_riverpod/CHANGELOG.md +++ b/packages/flutter_riverpod/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased fix + +- Fix `ChangeNotifierProvider` docs + ## 2.3.4 - 2023-04-07 - Fixes an issue with nested ProviderScope (thanks to @jeiea) diff --git a/packages/flutter_riverpod/lib/src/change_notifier_provider/base.dart b/packages/flutter_riverpod/lib/src/change_notifier_provider/base.dart index f145af15f..d19900f75 100644 --- a/packages/flutter_riverpod/lib/src/change_notifier_provider/base.dart +++ b/packages/flutter_riverpod/lib/src/change_notifier_provider/base.dart @@ -26,25 +26,23 @@ abstract class ChangeNotifierProviderRef /// Using [ChangeNotifier], you could represent such state as: /// /// ```dart -/// class TodosNotifier extends ChangeNotifier> { -/// TodosNotifier(): super([]); +/// class TodosNotifier extends ChangeNotifier { +/// List todos = []; /// /// void add(Todo todo) { -/// state = [...state, todo]; +/// todos.add(todo); +/// notifyListeners(); /// } /// /// void remove(String todoId) { -/// state = [ -/// for (final todo in state) -/// if (todo.id != todoId) todo, -/// ]; +/// todos.removeWhere((todo) => todo.id == todoId); +/// notifyListeners(); /// } /// /// void toggle(String todoId) { -/// state = [ -/// for (final todo in state) -/// if (todo.id == todoId) todo.copyWith(completed: !todo.completed), -/// ]; +/// final todo = todos.firstWhere((todo) => todo.id == todoId); +/// todo.completed = !todo.completed; +/// notifyListeners(); /// } /// } /// ``` @@ -60,7 +58,7 @@ abstract class ChangeNotifierProviderRef /// ```dart /// Widget build(BuildContext context, WidgetRef ref) { /// // rebuild the widget when the todo list changes -/// List todos = ref.watch(todosProvider); +/// List todos = ref.watch(todosProvider).todos; /// /// return ListView( /// children: [ diff --git a/website/docs/providers/change_notifier_provider/todos.dart b/website/docs/providers/change_notifier_provider/todos.dart index da5db1dd0..d766920b1 100644 --- a/website/docs/providers/change_notifier_provider/todos.dart +++ b/website/docs/providers/change_notifier_provider/todos.dart @@ -32,12 +32,9 @@ class TodosNotifier extends ChangeNotifier { // Let's mark a todo as completed void toggle(String todoId) { - for (final todo in todos) { - if (todo.id == todoId) { - todo.completed = !todo.completed; - notifyListeners(); - } - } + final todo = todos.firstWhere((todo) => todo.id == todoId); + todo.completed = !todo.completed; + notifyListeners(); } }