'A non-null String must be provided to a Text widget' (Flutter)

半世苍凉 提交于 2021-01-29 18:12:15

问题


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),
              ),
            ],
          ),

How would one go about solving this?


回答1:


You can just add:

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

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.



来源:https://stackoverflow.com/questions/64343386/a-non-null-string-must-be-provided-to-a-text-widget-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!