user22969480
user22969480

Reputation:

Riverpod's best practices and approach for managing notifiers?

Riverpod's approach for managing notifiers? should be flexible, easy to manage, and best for performance.

  1. splitting them into smaller notifier?
  2. combining related state into a single class with a single notifier?

For example,

let's say I'm working on a product screen that displays men's products, women's products, and product categories. To manage the state of these, should I split them into smaller notifier or combine related states into a single class?

splitting them into smaller providers example -

class MenProductsListNotifier extends Notifier<List<Product>> {}

class WomenProductsListNotifier extends Notifier<List<Product>> {}

class ProductCategoriesNotifier extends Notifier<List<ProductCategories>> {}

combining related state into a single class with a single notifier ?

class ProductNotifier extends Notifier<ProductState> {}

class ProductState {
  List<Product> menProducts;
  List<Product> womenProducts;
  List<ProductCategories> productCategories;

  const ProductState({
    this.menProducts = const [],
    this.womenProducts = const [],
    this.productCategories = const [],
  });
}

Which one is recommended, and what are the best practices?

Upvotes: 1

Views: 124

Answers (1)

Ruble
Ruble

Reputation: 4844

Yes, you should separate them. The whole point is that when these particular lists have dependencies, all other lists and all widgets will be rebuilt.

Moreover, if any of the lists will change independently, then only the widget that depends on a particular notifier of a particular list will be rebuilt as well.

Upvotes: 0

Related Questions