必须为文本窗口小部件提供非空字符串。错误

时间:2020-04-24 10:48:29

标签: flutter dart

运行应用程序时出现此错误。我遵循了youtube教程,但在我的情况下,总是会出现错误:必须为Text小部件提供非空字符串。我已经尝试了多种方法,但没有任何反应。我不知道还能写些什么...但是如果不提供更详细的信息就无法发布问题。 我该如何解决这个问题?

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);
                            });
                          } ),
                    ),
                  ));
            });
      }),
    );
  }
}

1 个答案:

答案 0 :(得分:2)

您的documentSnapshot["todoTitle"]返回null,并且您不应该向null小部件提供任何Text值。因此,一种解决方案是使用类似

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