Flutter Firebase 方法 'data' 被调用为 null
Flutter Firebase The method 'data' was called on null
我正在使用 Firestore 并尝试通过 Streambuilder 获取流。
然而,这个错误发生了。
The following NoSuchMethodError was thrown building StreamBuilder<DocumentSnapshot<Object>>
(dirty, state: _StreamBuilderBaseState<DocumentSnapshot<Object>,
AsyncSnapshot<DocumentSnapshot<Object>>>#32fdb):
The method 'data' was called on null.
Receiver: null
Tried calling: data()
这是我的代码。
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class UserDetailPage extends StatefulWidget {
String uid;
UserDetailPage(this.uid);
@override
_UserDetailPageState createState() => _UserDetailPageState();
}
class _UserDetailPageState extends State<UserDetailPage> {
final List<String> datas = <String>['a', 'b', 'c', 'd', 'e', 'f', 'g', '1', '2','3', '4', '5', '6'];
CollectionReference userstream = FirebaseFirestore.instance.collection('users');
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('User Detail'),
),
body:_buildBody(),
);
}
_buildBody() {
return StreamBuilder(
stream: userstream.doc(widget.uid).snapshots(),
builder: (context, snapshot){
Map<String, dynamic> user_data =snapshot.data.data();
if(snapshot.hasError){
return Text('ERROR');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
return Padding(
padding: const EdgeInsets.all(20.0),
child: ListView.separated(
padding: EdgeInsets.only(left: 20, right: 20),
itemCount: 13,
separatorBuilder: (BuildContext context, int index) => const Divider(),
itemBuilder: (BuildContext context, int index){
return Center(
child: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(datas[index]),
Text(user_data[datas[index]] is int?user_data[datas[index]].toString():user_data[datas[index]])
],
),
),
);
}
)
);
},
);
}
}
有趣的是,在这个错误发生后,应用程序上立即出现了我想要的结果。
所以我认为问题出在 initstate() 但我不知道到底出了什么问题。
顺便说一句,这个页面是从
调用的
UserDetailPage( doc.get('uid')!=null?doc.get('uid'):'5AJUsH5LYaQcBiTtO5MA7d6OKx72');
AsyncSnapshot
包装异步加载的数据。调用 snapshot.data
而不检查数据是否可用(如您在下面的代码中所做的那样),意味着您忽略了这一事实,并且最好不要使用 StreamBuilder
:
stream: userstream.doc(widget.uid).snapshots(),
builder: (context, snapshot){
Map<String, dynamic> user_data =snapshot.data.data();
realtime listeners 上的 FlutterFire 文档中显示了处理流的正确方法。您所需的更改是您只在 所有检查之后调用 snapshot.data
,而不是在它们之前调用:
builder: (context, snapshot){
if(snapshot.hasError){
return Text('ERROR');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
Map<String, dynamic> user_data =snapshot.data.data();
我正在使用 Firestore 并尝试通过 Streambuilder 获取流。 然而,这个错误发生了。
The following NoSuchMethodError was thrown building StreamBuilder<DocumentSnapshot<Object>>
(dirty, state: _StreamBuilderBaseState<DocumentSnapshot<Object>,
AsyncSnapshot<DocumentSnapshot<Object>>>#32fdb):
The method 'data' was called on null.
Receiver: null
Tried calling: data()
这是我的代码。
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class UserDetailPage extends StatefulWidget {
String uid;
UserDetailPage(this.uid);
@override
_UserDetailPageState createState() => _UserDetailPageState();
}
class _UserDetailPageState extends State<UserDetailPage> {
final List<String> datas = <String>['a', 'b', 'c', 'd', 'e', 'f', 'g', '1', '2','3', '4', '5', '6'];
CollectionReference userstream = FirebaseFirestore.instance.collection('users');
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('User Detail'),
),
body:_buildBody(),
);
}
_buildBody() {
return StreamBuilder(
stream: userstream.doc(widget.uid).snapshots(),
builder: (context, snapshot){
Map<String, dynamic> user_data =snapshot.data.data();
if(snapshot.hasError){
return Text('ERROR');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
return Padding(
padding: const EdgeInsets.all(20.0),
child: ListView.separated(
padding: EdgeInsets.only(left: 20, right: 20),
itemCount: 13,
separatorBuilder: (BuildContext context, int index) => const Divider(),
itemBuilder: (BuildContext context, int index){
return Center(
child: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(datas[index]),
Text(user_data[datas[index]] is int?user_data[datas[index]].toString():user_data[datas[index]])
],
),
),
);
}
)
);
},
);
}
}
有趣的是,在这个错误发生后,应用程序上立即出现了我想要的结果。 所以我认为问题出在 initstate() 但我不知道到底出了什么问题。
顺便说一句,这个页面是从
调用的UserDetailPage( doc.get('uid')!=null?doc.get('uid'):'5AJUsH5LYaQcBiTtO5MA7d6OKx72');
AsyncSnapshot
包装异步加载的数据。调用 snapshot.data
而不检查数据是否可用(如您在下面的代码中所做的那样),意味着您忽略了这一事实,并且最好不要使用 StreamBuilder
:
stream: userstream.doc(widget.uid).snapshots(),
builder: (context, snapshot){
Map<String, dynamic> user_data =snapshot.data.data();
realtime listeners 上的 FlutterFire 文档中显示了处理流的正确方法。您所需的更改是您只在 所有检查之后调用 snapshot.data
,而不是在它们之前调用:
builder: (context, snapshot){
if(snapshot.hasError){
return Text('ERROR');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
Map<String, dynamic> user_data =snapshot.data.data();