flutter - 必须向 Text 小部件提供非空字符串。错误

标签 flutter dart

运行应用程序时出现此错误。我遵循了一个 youtube 教程,但在我的情况下,错误 A non-null String must be provided to a Text widget 总是出现。我已经尝试了多种方法,但没有任何 react 。我不知道我还能写什么......但我不能在不提供更详细信息的情况下发布问题。
我怎么解决这个问题?

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

void main() => runApp(MaterialApp(
  debugShowCheckedModeBanner: false,
        theme: ThemeData(
          brightness: Brightness.light,
          primaryColor: Colors.blue,
          accentColor: Colors.orange
        ),
    home: MyApp(),
));

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}


class _MyAppState extends State<MyApp> {

  List todos = List();
  String input = "";


  createTodos() {
    DocumentReference documentReference =
        Firestore.instance.collection("MyTodos").document(input);

    //Map
    Map<String, String> todos = {"todoTitle": input};

    documentReference.setData(todos).whenComplete(() {
      print("$input created");
    });
  }

  deleteTodos() {

  }




  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My ToDos'),
        ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text('Add ToDo', style: TextStyle(fontWeight: FontWeight.bold),),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(15)
                ),
                content: TextField(
                  onChanged: (String value) {
                    input = value;
                  },
                ),
                actions: <Widget>[
                  FlatButton(
                      onPressed: () {
                        createTodos();

                        Navigator.of(context).pop();
                      },
                      child: Text('Add'))
                ],
              );
            });
        },
        child: Icon(
            Icons.add,
            color: Colors.white,
        ),
      ),
      body: StreamBuilder(
          stream: Firestore.instance.collection("MyTodos").snapshots(),
          builder: (context, snapshots){

            if(snapshots.data == null) return CircularProgressIndicator();

        return ListView.builder(
          shrinkWrap: true,
            itemCount: snapshots.data.documents.length,
            itemBuilder: (context, index) {
              DocumentSnapshot documentSnapshot = snapshots.data.documents[index];
              return Dismissible(
                  key: Key(index.toString()),
                  child: Card(
                    elevation: 4,
                    margin: EdgeInsets.all(8),
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10)
                    ),
                    child: ListTile(
                      title: Text(documentSnapshot["todoTitle"]),
                      trailing: IconButton(
                          icon: Icon(
                              Icons.delete),
                          color: Colors.red,
                          onPressed: (){
                            setState(() {
                              todos.removeAt(index);
                            });
                          } ),
                    ),
                  ));
            });
      }),
    );
  }
}

最佳答案

您的 documentSnapshot["todoTitle"]正在返回 null你不应该提供任何null值为 Text小部件。所以,一个解决方案是使用类似的东西

Text(documentSnapshot["todoTitle"] ?? "No title found") 

关于flutter - 必须向 Text 小部件提供非空字符串。错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61406608/

相关文章:

Flutter Web 卡在初始加载屏幕

enums - 枚举尝试失败

regex - 正则表达式可分别在任何地方查找匹配项

flutter - 从文件名字符串获取文件扩展名

flutter - Flutter中 float 操作按钮的放置

navigation - 如何 - 带有持久子屏幕的 Flutter Drawer

android - 从 Flutter 打开 Android Activity 和 iOS ViewController

flutter - 类参数设置中的多个if语句

flutter - 如何将参数发送到嵌入式 Flutter Web 应用程序?

flutter - 遍历列表并使用它初始化另一个变量