Firebase 颤振-NoSuchMethodError:方法';[]和#x27;被调用为空
我在尝试使用FutureBuilder从Firestore获取数据时遇到错误。如果用户登录并打印(snapshot.data),我将从firestore中获取正确的快照:Firebase 颤振-NoSuchMethodError:方法';[]和#x27;被调用为空,firebase,flutter,dart,google-cloud-firestore,widget,Firebase,Flutter,Dart,Google Cloud Firestore,Widget,我在尝试使用FutureBuilder从Firestore获取数据时遇到错误。如果用户登录并打印(snapshot.data),我将从firestore中获取正确的快照: flutter: FirebaseUser({displayName: null, providerId: Firebase, uid: UFubdB2xPCTUu8ESgQikOr9WOe52, phoneNumber: null, isEmailVerified: false, providerData: [{email:
flutter: FirebaseUser({displayName: null, providerId: Firebase, uid: UFubdB2xPCTUu8ESgQikOr9WOe52, phoneNumber: null, isEmailVerified: false, providerData: [{email: titi@momo.com, providerId: password, photoUrl: null, displayName: null, uid: titi@momo.com, phoneNumber: null}], email: titi@momo.com, photoUrl: null, lastSignInTimestamp: 1576367769805, isAnonymous: false, creationTimestamp: 1576367424402})
但是,如果导航到提要屏幕,则在打印时会引发异常(snapshot.data['email'])
主页导航
class MyApp extends StatelessWidget {
Widget _getScreenId() {
return StreamBuilder<FirebaseUser>(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
print(snapshot.data);
return Landing(userId: snapshot.data.uid);
} else {
return Login();
}
});
}
用户模型
User(
{this.id,
this.email,
this.password,
this.fullname,
this.username,
this.mobile,
this.profileImgUrl,
this.bio});
factory User.fromDoc(DocumentSnapshot doc) {
return User(
id: doc.documentID,
email: doc['email'],
fullname: doc['fullname'],
username: doc['username'],
mobile: doc['mobile'],
profileImgUrl: doc['profileImgUrl'],
bio: doc['bio'] ?? ''
);
}
我已经尝试了几个选项,希望能得到一些帮助。期货并不总是准备好的,它们是异步的。我想至少插入一个空检查,但这里有一个更好的方法。最好检查
未来
的连接状态
,检查它是否准备就绪,然后检查数据是否为空。使用ConnectionState
的另一个好处是能够检查它是否正在等待。然后,您可以显示加载图标或其他内容
class _FeedState extends State<Feed> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 1,
title: Text('feed screen'),
),
backgroundColor: Colors.white,
body: FutureBuilder(
future: usersRef.document(widget.userId).get(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
print(snapshot.data['email']);
return Padding(
padding: const EdgeInsets.only(top: 70, left: 20, right: 20),
child: Column(
children: <Widget>[
Text('MeandYou')
],
),
);
}
}
},
),
);
}
}
class\u FeedState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
背景颜色:Colors.white,
立面图:1,
标题:文本(“提要屏幕”),
),
背景颜色:Colors.white,
正文:未来建设者(
future:usersRef.document(widget.userId).get(),
生成器:(BuildContext上下文,异步快照){
if(snapshot.connectionState==connectionState.done){
if(snapshot.hasData){
打印(快照数据['email']);
返回填充(
填充:仅限常量边集(顶部:70,左侧:20,右侧:20),
子:列(
儿童:[
文本('MeandYou')
],
),
);
}
}
},
),
);
}
}
我尝试了此操作,但仍然出现错误<代码>颤振:类“DocumentSnapshot”没有实例获取程序“isNotEmpty”。flatter:Receiver:DocumentSnapshot的实例flatter:trusted calling:isNotEmptyWhoops实际上是snapshot.hasdata更好的方法是为类使用流和业务逻辑代码。流监视事件并处理这些事件,然后返回数据。这是Marcus Ng的教程吗?我也有同样的问题…@小朝阳是的。请检查下面的解决方案。
children: <Widget>[
Home(),
Feed(userId: widget.userId),
Profile(),
],
class Feed extends StatefulWidget {
final String userId;
Feed({this.userId});
@override
_FeedState createState() => _FeedState();
}
class _FeedState extends State<Feed> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 1,
title: Text('feed screen'),
),
backgroundColor: Colors.white,
body: FutureBuilder(
future: usersRef.document(widget.userId).get(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
print(snapshot.data['email']);
return Padding(
padding: const EdgeInsets.only(top: 70, left: 20, right: 20),
child: Column(
children: <Widget>[
Text('MeandYou')
],
),
);
},
),
);
}
}
final usersRef = _firestore.collection('users');
User(
{this.id,
this.email,
this.password,
this.fullname,
this.username,
this.mobile,
this.profileImgUrl,
this.bio});
factory User.fromDoc(DocumentSnapshot doc) {
return User(
id: doc.documentID,
email: doc['email'],
fullname: doc['fullname'],
username: doc['username'],
mobile: doc['mobile'],
profileImgUrl: doc['profileImgUrl'],
bio: doc['bio'] ?? ''
);
}
class _FeedState extends State<Feed> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 1,
title: Text('feed screen'),
),
backgroundColor: Colors.white,
body: FutureBuilder(
future: usersRef.document(widget.userId).get(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
print(snapshot.data['email']);
return Padding(
padding: const EdgeInsets.only(top: 70, left: 20, right: 20),
child: Column(
children: <Widget>[
Text('MeandYou')
],
),
);
}
}
},
),
);
}
}