【问题标题】:How do I go about synchronously initialising my provider? (Riverpod)如何同步初始化我的提供程序? (河足)
【发布时间】:2020-12-14 13:50:59
【问题描述】:

我有这样的提供者:

final profileImagesProvider =
    ChangeNotifierProvider.family<ProfileImagesNotifier, List<String>>(
        (ref, profileImages) => ProfileImagesNotifier(profileImages));

class ProfileImagesNotifier extends ChangeNotifier {
  ProfileImagesNotifier(List<String> images);

  List<String> images;
}

但是,当我尝试使用提供程序时:

Widget build(BuildContext context, ScopedReader watch) {
  var profileImages = watch(ProfileImagesNotifier(['test_1.png', 'test_2.png']))
  print(profileImages.images) //null
}

列表被检索为空。

我这样做对吗? (在河流 pod 和状态管理方面,我完全是个菜鸟)。

【问题讨论】:

  • 您是否尝试过使用ProfileImagesNotifier(List&lt;String&gt; this.images); 而不是ProfileImagesNotifier(List&lt;String&gt; images);
  • @GilesCorreiaMorton 就是这样。我怎么没看到,我不知道。
  • 没问题,很高兴它成功了!这是一个非常容易犯的错误,尤其是在学习如何构建和实施 Riverpod 状态管理实践时:)

标签: flutter state-management riverpod


【解决方案1】:

错误地调用了构造函数。

应该是:

ProfileImagesNotifier(this.images);

而不是

ProfileImagesNotifier(List<String> images);

【讨论】:

    猜你喜欢
    • 2019-10-24
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    相关资源
    最近更新 更多