我的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(),
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句