收到的错误:
NoSuchMethodError: invalid member on null: 'collection'
我正在使用Flutter Provider Package 4.3.2+2进行复杂的状态管理。
这是提供程序类:
class ClientsProvider extends ChangeNotifier {
FirebaseFirestore _fs;
StreamSubscription<QuerySnapshot> _stream;
List<QueryDocumentSnapshot> clients = [];
ClientsProvider() {
_stream = _fs.collection("clients").snapshots().listen((event) {//THIS IS WHERE THE ERROR POINTS
clients = event.docs;
notifyListeners();
});
}
@override
void dispose() {
super.dispose();
_stream.cancel();
}
}
这是父窗口小部件:
class _ParentState extends State<Parent> {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<ClientsProvider>(
create: (context) => ClientsProvider()),
ChangeNotifierProvider<MyProvider>(
create: (context) => MyProvider()),
],
child: Dashboard(),//This widget contains the Child() widget two widgets down.
);
}
}
这是Firebase快照更新时需要更新的子窗口小部件:
class _ChildState extends State<Child> {
@override
Widget build(BuildContext context) {
return Column(
children: [
_buildAddNew(),
Consumer<ClientsProvider>(
builder: (context, clientProvider, child) {
return Container(child: Text(clientProvider.clients.toString()));
},
),
],
);
}
}
再次,收到错误:
NoSuchMethodError: invalid member on null: 'collection'
数据不为空,为什么会收到此错误?
答案 0 :(得分:2)
_fs
为空,因为您从未分配过它。如documentation所示,也许您打算这样做:
FirebaseFirestore _fs = FirebaseFirestore.instance;