【问题标题】:Flutter Provider dependencyFlutter 提供者依赖
【发布时间】:2021-09-23 08:56:49
【问题描述】:

如何创建依赖于另一个提供者的提供者?

基本上,在我的用户登录后,主屏幕需要显示 2 个不同的信息:

  • 他的用户个人资料名称,
  • 他当前一周的信息来自 9 周的计划。

当前周位于他的用户配置文件中,因此我首先需要它才能使用 currentWeekProvider。我们如何才能实现这种排序和依赖关系?

然后我有两个提供者:

final userProfileProvider = FutureProvider<UserProfile>((ref) async {
  final repository = ref.watch(hopeRepositoryProvider);
  UserProfile user = await repository.getUserProfile();  
  return user;
  
});
final programProvider = FutureProvider<HopeProgram>((ref) async {
  final repository = ref.read(hopeRepositoryProvider);
  final user = ref.watch(userProfileProvider);  
  HopeProgram program = await repository.getCurrentWeekProgram(user.currentWeek); // does not work, the compile complain
  return program;
});
``

【问题讨论】:

  • 你试过 ProxyProvider 了吗?

标签: flutter flutter-provider riverpod


【解决方案1】:

谢谢雷米,

这是关键:

final user = await ref.watch(userProfileProvider.future)

【讨论】:

    【解决方案2】:

    正确的语法是:

    final programProvider = FutureProvider<HopeProgram>((ref) async {
      final repository = ref.read(hopeRepositoryProvider);
      final user = await ref.watch(userProfileProvider.future);  
      HopeProgram program = await repository.getCurrentWeekProgram(user.currentWeek); // does not work, the compile complain
      return program;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 2019-06-07
      • 2021-04-23
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多