gpt4 book ai didi

firebase - FutureBuilder 在显示结果之前显示错误

转载 作者:行者123 更新时间:2023-12-04 00:59:41 28 4
gpt4 key购买 nike

我的futurebuilder 在屏幕上显示错误几秒钟,然后显示结果这是错误日志:

The following NoSuchMethodError was thrown building FutureBuilder<DocumentSnapshot>(dirty, state: _FutureBuilderState<DocumentSnapshot>#fd0c5):
The method '[]' was called on null.
Receiver: null
Tried calling: []("title")

The relevant error-causing widget was:
FutureBuilder<DocumentSnapshot> file:///C:/Users/Admin/Desktop/Android/doorstep/lib/domain/repository/booking-repository.dart:7:12
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 BookingRepository.getTitle.<anonymous closure> (package:doorstep/domain/repository/booking-repository.dart:11:39)
#2 _FutureBuilderState.build (package:flutter/src/widgets/async.dart)
#3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)
#4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)

代码

Widget getTitle(String id) {
return FutureBuilder(
future: Firestore.instance.collection('products').document(id).get(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot){
return ListTile(
title: Text("${snapshot.data['title'].toString()}"),
);
}
);
}

最佳答案

试试

FutureBuilder<AsyncSnapshot<DocumentSnapshot>>(
builder: (context, snap) {
if (snap.connectionState != ConnectionState.done) {
//print('project snapshot data is: ${snap.data}');
return Text("loading");
} else {
if (snap.hasError) {
return Text([snap.error.toString()]);
}
else {
if (snap.hasData) {


return Text("${snap.data['title'].toString()}");
} else {
return Text("No DAta");
}
}
}
},
future:
Firestore.instance.collection('products').document(id).get(),
);

关于firebase - FutureBuilder 在显示结果之前显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59642161/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com