将 Flutter 与 Riverpod 和 null 安全一起使用时出错

问题描述

我第一次将 Flutter 与 Riverpod 和 null 安全一起使用,我有以下提供程序应该返回 ImageProfile 或 null。

final myProfileImageProvider = Provider.autodispose.family<ProfileImage?,int>(
  (ref,index) {
    final images = ref.watch(editMyProfileviewmodelProvider.state).images;
    if (index < images.length) {
      return images[index];
    }
    return null;
  },name: 'myProfileImageProvider',);

我正在这样的函数中读取提供者:

final profileImage = context.read<ProfileImage>(myProfileImageProvider(profileImageIndex));

但我收到以下编译器错误

The argument type 'AutodisposeProvider<ProfileImage?>' can't be assigned to the parameter 
type 'ProviderBase<Object,ProfileImage?>' because 'ProfileImage?' is nullable and 'Object' 
isn't.

请注意,在函数内部,我不能使用 watch 或 useProvider。我必须使用context.read。请注意 useProvider() 确实解决了问题,但不能在函数中使用它。问题是在函数内部读取提供程序时。

有人知道答案吗? 我很感激任何帮助。 谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)