Flutter错误-断言失败:第213行pos 15行:'data!= null':在从Firestore中获取数据时不正确 [英] Flutter error - failed assertion: line 213 pos 15: 'data != null': is not true at the time of fetching data from firestore
问题描述
使用flutter开发Android应用。试图从Firestore&通过小部件显示在屏幕上。这是我的代码...
Working on Android app using flutter. Trying to fetch documents from firestore & show on the screen through a widget. Here is my code...
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,
],
),
);
}
}
但是,无论何时我执行此小部件出现以下错误...
But, whenever I'm executing this widget I'm getting following error...
'package:flutter/src/widgets/text.dart': Failed assertion: line 213 pos 15: 'data != null': is not true
了解出了什么问题。
推荐答案
此错误是因为您试图向Text添加一些空数据。
用于将文本内容添加到文本小部件中,以动态方式更好地设置为获取数据的文本位置的setState。延迟获取内容会导致此错误。
This error is because you are trying to add some null data to Text. for adding text Content to Text widget dynamically better to setState of text place where you are getting that data. Delay for getting content cause this error. so a condition check for data is also good.
@override
void initState() {
setState(() {
docText=document['docTitle'];
});
super.initState();
}
....
title: new Text( docText == null? 'Loading ..',document['docTitle']),
尝试这种方式
这篇关于Flutter错误-断言失败:第213行pos 15行:'data!= null':在从Firestore中获取数据时不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!