Null check operator used on a null value waiting for the network image to load

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

Null check operator used on a null value waiting for the network image to load

问题

当我用 Firebase 用户登录时,我希望在首页抽屉中显示他的个人资料图片,该图片来自 Firestore 的 URL 字段。我想将这个图片显示在一个圆形头像部件中,所以我编写了一个函数来获取 URL,并将其提供给 NetworkImage():

String? stringUrl;

Future<String?> getData() async{
  var url = await FirebaseFirestore.instance
      .collection('users')
      .doc(currentUserUid)
      .get();
  setState(() {
    stringUrl = url['photourl'];
  });
  return null;
}

然后,我将这个字符串 URL 提供给圆形头像部件的 NetworkImage():

UserAccountsDrawerHeader(
  accountName: _getFieldDataToText(context, fieldName: 'name', currentUserUid: currentUserUid),
  accountEmail: _getFieldDataToText(context, fieldName: 'email', currentUserUid: currentUserUid),
  currentAccountPicture: CircleAvatar(
    backgroundImage: NetworkImage(stringUrl!),
  ),
),

但是当我运行我的应用程序并输入用户名和密码登录时,登录后的一瞬间我得到一个红屏,显示错误信息 "Null check operator used on a null value",然后它立即消失,应用程序继续正常工作。

我认为这是因为图片还没有重新加载,所以我得到了一个空值?我不知道问题出在哪里,无法找出原因,你能帮我吗?

我检查了代码中的每个异步和等待,没有错误。

英文:

when i sign in with a firebase user i want his profile picture to show in the home page drawer from the firestore url field, so and show in a circle avatar widget, so i wrote a function to get the url as a string and provide it to the Network image(),

String? stringUrl;

  Future&lt;String?&gt; getData() async{
    var url = await FirebaseFirestore.instance
        .collection(&#39;users&#39;)
        .doc(currentUserUid)
        .get();
    setState(() {
      stringUrl= url[&#39;photourl&#39;];
    });
    return null;
  }

then i provide this String url to the network image in the circle avatar widget

UserAccountsDrawerHeader(
              accountName: _getFieldDataToText(context, fieldName: &#39;name&#39;, currentUserUid: currentUserUid) ,
              accountEmail: _getFieldDataToText(context, fieldName: &#39;email&#39;, currentUserUid: currentUserUid) ,
              currentAccountPicture: CircleAvatar(
                backgroundImage: NetworkImage(stringUrl!),
              ),
            ),

but when i run my app and write my user and pw to login, just after loging in i get a red screen with this error "Null check operator used on a null value" for a second and it disappears and the app continue to work normally.

i think it's because the image is not yet reloaded so i get a null value ? i don't know whats the problem and i can't figure it out , can you help me?

i checked every async and await in the code and there is no errors.

答案1

得分: 1

当你在可为空的变量后面加上 ! 时,你在说:“我确定这个值现在不为空”。当Flutter在实际上是 null 的值上找到这个时,它会引发你收到的错误。

的确,当数据从数据库加载时,你的 stringUrl 变量将为null。在此期间,你可能不希望在UI中显示任何图像,所以你可以像这样处理它:

currentAccountPicture: stringUrl != null
  ? CircleAvatar(backgroundImage: NetworkImage(stringUrl!)),
  : CircularProgressIndicator(),
英文:

When you put a ! after a nullable variable you are saying "I know for certain this value is not null right now". When Flutter finds this on a value that in fact is null, it raises the error you get.

Indeed, your stringUrl variable will be null while the data is being loaded from the database. During that time you probably don't want to show any image in the UI, so you could for example handle it like this:

currentAccountPicture: stringUrl != null
  ? CircleAvatar(backgroundImage: NetworkImage(stringUrl!)),
  : CircularProgressIndicator(),

答案2

得分: 0

以下是要翻译的内容:

做这个,更有效。

backgroundImage: stringUrl != null ? NetworkImage(stringUrl) : AssetImage('your/image/path'),

英文:

Do this Instead, More effective.

 backgroundImage: stringUrl != null ? NetworkImage(stringUrl) : AssetImage(&#39;your/image/path&#39;),

答案3

得分: 0

当应用程序首次打开时,由于尚无法从服务器检索数据,您提供给该字段的变量将变为null。稍后,当数据到达时,它会填充该字段,错误消息会消失。如果您不希望出现红屏幕,您可以在数据到达之前在该字段中显示进度指示器。

backgroundImage: stringUrl == null ? CircularProgressIndicator() : NetworkImage(stringUrl);
英文:

When the application is first opened, the variable you give to the field becomes null because you cannot yet retrieve the data from the server. Later, when the data arrives, it fills the field and the error message disappears. If you don't want the red screen to appear, you can show a progress indicator in the field until the data arrives.

backgroundImage: stringurl ==null?CircularProgressIndicator():NetworkImage(stringUrl);

huangapple
  • 本文由 发表于 2023年5月6日 20:33:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188912.html
匿名

发表评论

匿名网友

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

确定