【问题标题】:A non-null String must be provided to a Text widget. Error必须向 Text 小部件提供非空字符串。错误
【发布时间】:2020-04-24 10:48:29
【问题描述】:

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

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"]返回空值,使用Text(documentSnapshot["todoTitle"] ?? "None")
  • 非常感谢您解决了我的问题

标签: flutter dart


【解决方案1】:

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

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

【讨论】:

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