StreamBuilder(
stream: firestore
.collection('users')
.document(user.uid)
.collection("notes")
.snapshots(),
builder: (context, snapshot) {
QuerySnapshot snap = snapshot?.data;
List<DocumentSnapshot> notes = snap?.documents;
return ListView.builder(
physics: AlwaysScrollableScrollPhysics(),
itemCount: notes?.length,
itemBuilder: (BuildContext context, int index) {
DocumentSnapshot doc = notes[index];
Map body = doc.data;
return Dismissible(
key: ObjectKey("${notes[index]}"),
background: stackBehindDismiss(),
direction: DismissDirection.endToStart,
onDismissed: (v) {
deleteNote(doc.documentID);
},
child: Padding(
padding: EdgeInsets.all(10),
child: InkWell(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => DisplayScreen(
title: body,
docId: doc,
),
),
);
},
child: _buildNotes(
context,
"${body["title"]}",
"${body["content"]}"),
),
),
);
},
);
}),
这是我得到的错误
“以下NoSuchMethodError被抛出: 方法'[]'在null上被调用。 接收者:null 尝试致电:
引发异常时,这是堆栈: #0 Object.noSuchMethod(dart:core-patch / object_patch.dart:51:5)
#1 _HomeScreenState.build .. (软件包:firebase_note_app / screens / homescreen.dart:75:59)
#2 SliverChildBuilderDelegate.build(package:flutter / src / widgets / sliver.dart:448:22)
#3 SliverMultiBoxAdaptorElement._build。 (软件包:flutter / src / widgets /sliver.dart:1136:67)
#4 _HashMap.putIfAbsent(dart:collection-patch / collection_patch.dart:140:29) ...
”
StackTrace指向“ DocumentSnapshot doc = notes [index];”
答案 0 :(得分:0)
看起来像是错别字,您可以使用ctrl + f来搜索该键[]
答案 1 :(得分:0)
notes
是null
。您需要检查快照中是否有数据,并显示快照中是否有数据。
builder: (context, snapshot) {
if(snapshot.hasData) {//Check if there is data
QuerySnapshot snap = snapshot?.data;
List<DocumentSnapshot> notes = snap?.documents;
return ListView.builder(
physics: AlwaysScrollableScrollPhysics(),
itemCount: notes?.length,
itemBuilder: (BuildContext context, int index) {
DocumentSnapshot doc = notes[index];
Map body = doc.data;
return Dismissible(
key: ObjectKey("${notes[index]}"),
background: stackBehindDismiss(),
direction: DismissDirection.endToStart,
onDismissed: (v) {
deleteNote(doc.documentID);
},
child: Padding(
padding: EdgeInsets.all(10),
child: InkWell(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => DisplayScreen(
title: body,
docId: doc,
),
),
);
},
...
}
else {//If there isn't data
//Return what you want to show before the stream emits its first event
return CircularProgressIndicator();
}