flutter - Flutter:即使包含数据,StreamProvider 也会在列表中返回 null
问题描述
我正在尝试在 Firestore 服务器和我的应用程序之间建立一个流。我正在尝试从服务器检索笔记列表以显示在ListView
小部件中。但是,我不断收到一条错误消息,指出返回的列表为空。通过调试,我注意到快照确实包含我试图访问的数据,所以在_notesFromSnapshot
函数能够将每个快照映射到Note
对象之前发生了一些错误,这让我相信应用程序ListView
在Stream 有足够的时间来获取所有数据。
编辑:如果我将 StreamProvider 更改为收听QuerySnapshot
而不是笔记列表,则该列表有效,但我需要访问自定义对象的便利。
NoteListScreen.dart
class NoteListScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
return Scaffold(
appBar: NavBar(title: 'My notes'),
body: StreamProvider<List<Note>>.value(
value: DatabaseService(uid: user.uid).notes,
child: NoteList(),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context).pushNamed('/note/create');
},
child: Icon(Icons.add),
),
);
}
}
NoteList.dart
class NoteList extends StatefulWidget {
@override
State<StatefulWidget> createState() => _NoteListState();
}
class _NoteListStateextends State<NoteList> {
@override
Widget build(BuildContext context) {
final notes = Provider.of<List<Note>>(context);
return ListView.builder(
itemCount: notes.length,
^^^^^^^^^^^^ Returns error: 'The getter 'length' was called on null.'
itemBuilder: (context, index) {
return Card(
child: NoteItem(
name: notes[index].name,
),
);
},
);
}
}
数据库服务.dart
class DatabaseService {
final String uid;
DatabaseService({this.uid});
// collection reference
final CollectionReference _userCollection = Firestore.instance.collection('users');
.....
List<Note> _notesFromSnapshot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
return Note(
name: doc.data['name'],
);
});
}
Stream<List<Note>> get notes {
return _userCollection
.document(uid)
.collection('notes')
.snapshots()
.map(_notesFromSnapshot);
}
}
解决方案
我通过.toList()
在此函数的末尾添加来解决问题:
List<Note> _notesFromSnapshot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
return Note(
name: doc.data['name'],
);
}).toList();
}
推荐阅读
- swift - 如何使用 SwiftUI 和 CoreData (@FetchRequest) 对动态列表排序进行动画处理
- flutter - Flutter Web 中的 OAuth2?
- media-source - Mediasource 支持 H265 吗?
- nativescript - iOS14 和 Xcode12 的 Nativescript 问题
- r - 提取节点集合的邻居名称作为列表
- python - 如何停止屏幕滚动并开始移动角色
- sql - Postgresql,一个函数调用另一个函数并向调用者获取 2 个输出值
- c - 从一组 8 位数字中解码 11 位数字
- python - 六边形中不同的颜色线
- powershell - foreach 循环将文件复制到 c:\users\ 中的所有目录中......希望是一个简单的