【问题标题】:'A non-null String must be provided to a Text widget' (Flutter)'必须向 Text 小部件提供非空字符串' (Flutter)
【发布时间】:2021-01-28 06:34:36
【问题描述】:

我已尝试使用以下代码解决此问题,但恐怕该错误仍然普遍存在:

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]

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

你可以添加:

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

上面会检查左边的表达式是否不为空,如果是则“空”将被添加到文本小部件中。您还必须将条件添加到其他 Text 小部件。

【讨论】:

    猜你喜欢
    • 2020-04-14
    • 1970-01-01
    • 2020-11-18
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 2021-03-03
    相关资源
    最近更新 更多