Riverpod需要注册多少个提供者

huangapple go评论81阅读模式
英文:

Riverpod how many providers to register

问题

以下是您要求的翻译:

我正在将我的提供者转换为riverpod

目前,一个提供者看起来像这样:

class ExampleProvider extends ChangeNotifier {
    Future<void> loadData() {
       ...
       notifyListener();
    }

    Future<void> filterItem() {
       ...
       notifyListener();
    }

    Future<void> getCustomer(int id) {
       ...
       notifyListener();

    }
    //等等
}

所以你可以说这个提供者执行不同类型的操作。

在使用Riverpod时,创建一个大的状态类是否不好:

class ExampleState {
  final Customer details;
  final String error;
  final bool isLoading;
  final List<Customer> customersList;
  final List<Customer> filteredCustomers;
  ....
  copyWith....
}

然后也做一个riverpod提供者,像这样:

final exampleProvider =
    StateNotifierProvider<ExampleNotifier, ExampleState>((ref) {
  return ExampleNotifier();
});

还是应该将所有东西拆分开,分成一个

> exampleFilterProvider / exampleCustomerProvider / exampleLoadDataProvider

当然,这会提高可读性,但它是否也会提升性能?

tl;dr
对于应用程序中属于同一个功能的各个功能,是否为每个功能都创建一个自定义的Riverpod提供者会提升性能

最好的问候
英文:

I am in the process of converting my providers to riverpod.<br />

Currently a Provider looks like this:

class ExampleProvider extends ChangeNotifier {
    Future&lt;void&gt; loadData() {
       ...
       notifyListener();
    }

    Future&lt;void&gt; filterItem() {
       ...
       notifyListener();
    }

    Future&lt;void&gt; getCustomer(int id) {
       ...
       notifyListener();

    }
    //And so on
}

So you could say this Provider does different types of actions.<br />

When using Riverpod is it bad practice to create a big State Class e.g Like so:

class ExampleState {
  final Customer details;
  final String error;
  final bool isLoading;
  final List&lt;Customer&gt; customersList;
  final List&lt;Customer&gt; filteredCustomers;
  ....
  copyWith....
}

And then doing also one riverpod provider like so:

final exampleProvider =
    StateNotifierProvider&lt;ExampleNotifier, ExampleState&gt;((ref) {
  return ExampleNotifier();
});

or would I split everything up and having an

> exampleFilterProvider / exampleCustomerProvider / exampleLoadDataProvider

Of course this would improve readability but does it also improve the performance?

tl;dr
Does it improve performance to have a custom Riverpod provider for each function in our app even tho the functions belong to the same feature?

Best Regards

答案1

得分: 1

只返回翻译好的部分:

如果它们负责不同的功能,拥有多个提供者将是合理的 - 那么我们可以使用 ProviderStateProviderFutureProviderStreamProvider。借助这些提供者,您可以轻松地精确控制重建。

Widget build(BuildContext context, WidgetRef ref) {
  String name = ref.watch(userNameProvider);
  return Text(name);
}

如果您有许多不同的字段,但它们提供共同的功能 - 那么请使用 NotifierAsyncNotifierStreamNotifier(或传统的提供者 - ChangeNotifierProviderStateNotifierProvider)与 ExampleState 模型。如果模型中至少有一个字段发生变化,那么所有监听器都将被通知,您会同意,这不再那么酷了。不过,您可以使用 select 来过滤复杂对象中的变化:

Widget build(BuildContext context, WidgetRef ref) {
  String name = ref.watch(userProvider.select((user) => user.name));
  return Text(name);
}

但如果您的模型足够复杂,这可能会过于冗余。

英文:

It will be logical to have several providers if they are responsible for different functionality - then we use Provider, StateProvider, FutureProvider or StreamProvider. With the help of these providers, you can easily control your rebuilds in a pinpoint manner.

Widget build(BuildContext context, WidgetRef ref) {
  String name = ref.watch(userNameProvider);
  return Text(name);
}

If you have many different fields but they provide common functionality - then use Notifier, AsyncNotifier, StreamNotifier (or legacy providers - ChangeNotifierProvider and StateNotifierProvider) with ExampleState model. If at least one field changes in the model, then all listeners will be notified that you agree, it’s not so cool anymore. However, you can use select to filter changes in a complex object:

Widget build(BuildContext context, WidgetRef ref) {
  String name = ref.watch(userProvider.select((user) =&gt; user.name));
  return Text(name);
}

But this can be overkill if your model is complex enough.

huangapple
  • 本文由 发表于 2023年6月22日 18:46:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定