Comments (7)
A similar question would be reflecting state changes when both the TodoList and Todo are state (or change) notifiers. It might be that the action of updating a todo's data belongs in a Todo state notifier so that subscribed widgets for that todo can rebuild, but we also need the todo state within the TodoList to be updated.
from riverpod.
The important thing is to have a single source of truth. So you should make sure that both todo-lists obtain their todos from same source.
This could be done by having a "family" for all todos.
Since you want them to be editable, you could use a StateNotifierProviderFamily
, and have the argument be the todo ID:
class TodoNotifier extends StateNotifier<Todo> {
TodoNotifier(String id): super(Todo(id: id));
void updateDescription(String description) {
state = state.copyWith(description: description);
}
}
final todoFamily = StateNotifierProviderFamily<TodoNotifier, String>((ref, id) {
return TodoNotifier(id);
});
From there, the provider for your home and for the other screen should both use todoFamily
to obtain their todos.
In the ideal world, they should both be a Computed. Otherwise, you can make them a state/change notifier, and they should use TodoNotifier.addListener
to keep track of changes on individual todos.
This way, both lists obtain their todos from the same source, which means that when a todo is updated, both lists will have the update.
from riverpod.
To expand on the previous point of having a single source of truth, it would make sense for your "home" and "other screen" lists to only store the id of a todo, not the todo itself.
So we would have:
final homeList = StateNotifierProvider((ref) => HomeList());
class HomeList extends StateNotifier<List<String>> {
HomeList(this.ref): super([]);
final ProviderReference ref;
void addTodo(String description) {
final newTodoId = _uuid.v4();
// Initialize the new todo
final todoNotifier = ref.read(todoFamily(newTodoId));
todoNotifier.updateDescription(description);
// update the UI
state = [...state, newTodoId];
}
}
Then the list of IDs can be combined with todoFamily
to obtain the todos. This could be done either in a Computed
or inside a Consumer directly.
final ids = read(homeList.state);
final todos = [
for (final id in ids)
read(todoFamily(id).state)
];
Which would automatically update the UI both when the id list changes or when the content of a todo changes
from riverpod.
Thank you for these detailed answers @rrousselGit – I'm going to have to spend a bit of time understanding them, especially as I'm still using Provider rather than Riverpod.
from riverpod.
To expand on the previous point of having a single source of truth, it would make sense for your "home" and "other screen" lists to only store the id of a todo, not the todo itself.
So we would have:
final homeList = StateNotifierProvider((ref) => HomeList()); class HomeList extends StateNotifier<List<String>> { HomeList(this.ref): super([]); final ProviderReference ref; void addTodo(String description) { final newTodoId = _uuid.v4(); // Initialize the new todo final todoNotifier = ref.read(todoFamily(newTodoId)); todoNotifier.updateDescription(description); // update the UI state = [...state, newTodoId]; } }Then the list of IDs can be combined with
todoFamily
to obtain the todos. This could be done either in aComputed
or inside a Consumer directly.final ids = read(homeList.state); final todos = [ for (final id in ids) read(todoFamily(id).state) ];Which would automatically update the UI both when the id list changes or when the content of a todo changes
In this case, how to determine homeList
is already disposed, or todoFamily(todoId)
is already disposed. (the list or the item is not on screen now)
Use case: need to know what list and items in screen (memory) if we push data from server to app.
from riverpod.
To expand on the previous point of having a single source of truth, it would make sense for your "home" and "other screen" lists to only store the id of a todo, not the todo itself.
So we would have:final homeList = StateNotifierProvider((ref) => HomeList()); class HomeList extends StateNotifier<List<String>> { HomeList(this.ref): super([]); final ProviderReference ref; void addTodo(String description) { final newTodoId = _uuid.v4(); // Initialize the new todo final todoNotifier = ref.read(todoFamily(newTodoId)); todoNotifier.updateDescription(description); // update the UI state = [...state, newTodoId]; } }Then the list of IDs can be combined with
todoFamily
to obtain the todos. This could be done either in aComputed
or inside a Consumer directly.final ids = read(homeList.state); final todos = [ for (final id in ids) read(todoFamily(id).state) ];Which would automatically update the UI both when the id list changes or when the content of a todo changes
In this case, how to determine
homeList
is already disposed, ortodoFamily(todoId)
is already disposed. (the list or the item is not on screen now)Use case: need to know what list and items in screen (memory) if we push data from server to app.
Is there any update on this?
from riverpod.
To expand on the previous point of having a single source of truth, it would make sense for your "home" and "other screen" lists to only store the id of a todo, not the todo itself.
So we would have:
final homeList = StateNotifierProvider((ref) => HomeList()); class HomeList extends StateNotifier<List<String>> { HomeList(this.ref): super([]); final ProviderReference ref; void addTodo(String description) { final newTodoId = _uuid.v4(); // Initialize the new todo final todoNotifier = ref.read(todoFamily(newTodoId)); todoNotifier.updateDescription(description); // update the UI state = [...state, newTodoId]; } }Then the list of IDs can be combined with
todoFamily
to obtain the todos. This could be done either in aComputed
or inside a Consumer directly.final ids = read(homeList.state); final todos = [ for (final id in ids) read(todoFamily(id).state) ];Which would automatically update the UI both when the id list changes or when the content of a todo changes
Would you provide an example of how to delete a todo item by its ID? Suppose we have two lists that fetch data from remote API but contain some overlapping todo items, and the user wants to delete an item so the change will be reflected in multiple todo lists.
Sorry I’m new to river-do and couldn’t seem to find a solution in the doc.
from riverpod.
Related Issues (20)
- examples/pub dependencies and auth problems HOT 1
- The subclass of AutoDisposeNotifier can always obtain internationalization, even if the phone's system Settings are Chinese, and finally English HOT 2
- Option to ignore previous state in AsyncValue by default HOT 3
- Support async `onDispose` HOT 3
- invalidation forwarding HOT 5
- docs: invalid use of `Uri.https` in readme HOT 1
- Read a HOT 1
- Read a "disposed" notifier HOT 1
- Refactoring Propositions dropping out when updating flutter HOT 2
- Add exception handling to 'ref.refresh()' HOT 6
- Add something like `ref.watchStream(...)` HOT 10
- ProviderScope state wrongly disposed possible bug HOT 5
- Add Japanese docs
- [riverpod_generator] `dependencies` does not respect `provider_family_name_suffix` option
- StateNotifierProvider always refreshs when invalidated problem still exists HOT 3
- Error: 'LintCode' is imported from both 'package /src/dart/error/lint_codes.dart' and 'package /src/lint_codes.dart' on Flutter 3.24.0 with build_runner HOT 3
- VS Code refactoring options are not available with Flutter 3.24 stable release HOT 4
- `initialValue`-like behaviour for the FutureProvider HOT 7
- `flutter` dependency included when using Riverpod in a server-side environment HOT 1
- ref.watch doesn't work HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from riverpod.