Flutter 错误 - 断言失败:第 213 行 pos 15:'data != null':从 firestore 获取数据时不为真

Sur*_*esh 11 dart flutter google-cloud-firestore

使用 Flutter 开发 Android 应用程序。尝试从 firestore 获取文档并通过小部件显示在屏幕上。这是我的代码...

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

class HomePage extends StatefulWidget {
  @override
  HomePageState createState() => new HomePageState();
}

class HomePageState extends State<HomePage> {

  @override
  void initState() {
    super.initState();
  }



  @override
  Widget build(BuildContext context) {

    Widget userTimeline = new Container(
        margin: const EdgeInsets.only(top: 30.0, right: 20.0, left: 20.0),
        child: new Row(
          children: <Widget>[
            new Expanded(
                child: new Column(
              children: <Widget>[
                new StreamBuilder<QuerySnapshot>(
                  stream: Firestore.instance.collection('tripsDocs').snapshots(),
                  builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
                    if (!snapshot.hasData) return new Text('Loading...');

                    new ListView(
                      children: snapshot.data.documents.map((DocumentSnapshot document) {
                        new ListTile(
                          title: document['docTitle'] != null? new Text(document['docTitle']) : new Text("Hello"),
                          subtitle: new Text('Suresh'),
                        );
                      }).toList(),
                    );
                  },
                )
              ],
            ))
          ],
        ));

    return new Scaffold(

      body: new ListView(
        children: <Widget>[
          userTimeline,
        ],
      ),

    );

  }
}
Run Code Online (Sandbox Code Playgroud)

但是,每当我执行这个小部件时,我都会收到以下错误...

'package:flutter/src/widgets/text.dart': Failed assertion: line 213 pos 15: 'data != null': is not true
Run Code Online (Sandbox Code Playgroud)

无法理解出了什么问题。

Phu*_*ran 2

这是 Text 的构造函数

const Text(this.data, {
    Key key,
    this.style,
    this.textAlign,
    this.textDirection,
    this.softWrap,
    this.overflow,
    this.textScaleFactor,
    this.maxLines,
  }) : assert(data != null),
       textSpan = null,
       super(key: key);
Run Code Online (Sandbox Code Playgroud)

最终字符串数据;

如您所见,data 是必填字段,并且不能为空。

如果您的数据可能为空,您可以使用下面的代码

title: document['docTitle'] != null? new Text(document['docTitle']) : new Text("Hello"),
Run Code Online (Sandbox Code Playgroud)