Coder Social home page Coder Social logo

Comments (7)

scopendo avatar scopendo commented on August 21, 2024

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.

rrousselGit avatar rrousselGit commented on August 21, 2024

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.

rrousselGit avatar rrousselGit commented on August 21, 2024

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.

scopendo avatar scopendo commented on August 21, 2024

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.

devon avatar devon commented on August 21, 2024

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

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.

JuI3s avatar JuI3s commented on August 21, 2024

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

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.

Is there any update on this?

from riverpod.

JuI3s avatar JuI3s commented on August 21, 2024

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

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.