我无法加载已登录用户上传到Firestore的数据: 我的代码没有错误,我在StreamBuilder中使用提供程序包: 代码为:
我正在使用支架主体中的_buildContents方法,以在主屏幕上显示Firestore数据,
class ReportsPage extends StatelessWidget {
Future<void> _signOut(BuildContext context) async {
try {
final auth = Provider.of<AuthBase>(context);
await auth.signOut();
} catch (e) {
print(e.toString());
}
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Report'),
actions: <Widget>[
FlatButton(
child: Text(
'Logout',
style: TextStyle(fontSize: 18.0, color: Colors.white),
),
onPressed: () => _confirmSignOut(context),
)
],
),
body:_buildContents( context),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => AddReportPage.show(context),
),
);
}
}
Widget _buildContents(BuildContext context) {
final database = Provider.of<Database>(context);
return StreamBuilder<List<Report>>(
stream: database.reportsStream(),
builder: (context,snapshot){
if(snapshot.hasData){
final report= snapshot.data;
final children = report.map((reports) => Text(reports.crimeType))
.toList();
return ListView(children: children);
}
if(snapshot.hasError){
return Center(
child: Text('Some Error Occurred'),
);
}
return Center(
child: CircularProgressIndicator(),
);
},
);
}
abstract class Database{
Stream<List<Report>> reportsStream();
Future<void> createReport(Report report);
}
String documentIdFormCurrentDate() => DateTime.now().toIso8601String();
class FirestoreDatabase implements Database {
FirestoreDatabase({@required this.uid}) : assert(uid != null);
final String uid;
final _service = FirestoreService.instance;
Future<void> createReport(Report report) async => _service.setData(
path: APIPath.report(uid, documentIdFormCurrentDate()),
data: report.toMap(),
);
//TODO: temporary code:
Stream<List<Report>> reportsStream() => _service.collectionStream(
path: APIPath.reports(uid),
builder: (data) => Report.fromMap(data),
);
}
class FirestoreService {
FirestoreService._();
static final instance = FirestoreService._();
Future<void> setData({String path, Map<String, dynamic> data}) async{
final reference = Firestore.instance.document(path);
await reference.setData(data);
}
Stream<List<T>> collectionStream<T>({
@required String path,
@required T builder(Map<String, dynamic> data),
}) {
final reference = Firestore.instance.collection(path);
final snapshots = reference.snapshots();
return snapshots.map((snapshot) =>
snapshot.documents.map((snapshot) => builder(snapshot.data))
.toList());
}
}
class Report {
Report({@required this.crimeType, @required this.description});
final String crimeType;
final String description;
factory Report.fromMap(Map<String, dynamic> data){
if(data=null){
return null;
}
final String crimeType= data['Crime Type'];
final String description = data['Description'];
return Report(
crimeType: crimeType,
description: description,
);
}
Map<String, dynamic> toMap(){
return {
'CrimeType': crimeType,
'Description' : description,
};
}
}
调试控制台为:表示: enter image description here
堆栈跟踪为:
#0 new Report.fromMap (package:onlinecrimereportingsystem/app/Home/Models/Report.dart:10:8)
#1 FirestoreDatabase.reportsStream.<anonymous closure> (package:onlinecrimereportingsystem/services/database.dart:30:40)
#2 FirestoreService.collectionStream.<anonymous closure>.<anonymous closure> (package:onlinecrimereportingsystem/services/firestore_services.dart:19:53)
#3 MappedListIterable.elementAt (dart:_internal/iterable.dart:417:31)
#4 ListIterator.moveNext (dart:_internal/iterable.dart:343:26)
#5 new List.from (dart:core-patch/array_patch.dart:38:29)
#6 new List.of (dart:core-patch/array_patch.dart:68:17)
#7 ListIterable.toList (dart:_internal/iterable.dart:211:44)
#8 FirestoreService.collectionStream.<anonymous closure> (package:onlinecrimereportingsystem/services/firestore_services.dart:20:14)
#9 _MapStream._handleData (dart:async/stream_pipe.dart:219:31)
#10 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:157:13)
#11 _rootRunUnary (dart:async/zone.dart:1198:47)
#12 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
#13 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
#14 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
#15 _DelayedData.perform (dart:async/stream_impl.dart:611:14)
#16 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:730:11)
#17 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:687:7)
#18 _rootRun (dart:async/zone.dart:1182:47)
#19 _CustomZone.run (dart:async/zone.dart:1093:19)
#20 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
#21 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
#22 _rootRun (dart:async/zone.dart:1190:13)
#23 _CustomZone.run (dart:async/zone.dart:1093:19)
#24 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
#25 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
#26 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#27 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)