“必须将非空字符串提供给文本窗口小部件"(Flutter) [英] 'A non-null String must be provided to a Text widget' (Flutter)

查看:50
本文介绍了“必须将非空字符串提供给文本窗口小部件"(Flutter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下代码修复此问题,但恐怕该错误仍然很普遍:

I've tried to fix this with the following code but I'm afraid the error is still prevalent:

The following assertion was thrown building:
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 360 pos 10: 'data != null'


child: Row(
            children: [
              Text(
                  (contact.data()['rating'] == null)
                      ? "n/a"
                      : contact.data()['rating'].toString(),
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.tealAccent)),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: CircleAvatar(
                  radius: 25,
                  backgroundImage: AssetImage("assets/girl2.jpg"),
                ),
              ),
              Spacer(),
              Text(
                contact.data()['name'],
                style: TextStyle(
                    fontWeight: FontWeight.w400, color: Colors.tealAccent),
              ),
              Spacer(),
              Text(
                contact.data()['location'],
                style: TextStyle(
                    letterSpacing: 1,
                    fontSize: 10,
                    fontWeight: FontWeight.w300,
                    color: Colors.tealAccent),
              ),
            ],
          ),

如何解决这个问题?

推荐答案

您可以添加:

contact.data()['rating'] ?? "empty" 

以上内容将检查左侧的表达式是否不为null,如果为空,则为空.将被添加到文本"小部件中.您还必须将条件添加到其他 Text 小部件中.

The above will check if the expression on the left is not null, if it is then "empty" will be added to the Text widget. You have to add the condition to the other Text widgets also.

这篇关于“必须将非空字符串提供给文本窗口小部件"(Flutter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆