【发布时间】:2021-07-03 13:34:02
【问题描述】:
出现以下错误: 请帮帮我。
The following NoSuchMethodError was thrown building FutureBuilder<DocumentSnapshot>(dirty, state: _FutureBuilderState<DocumentSnapshot>#7140d):
The getter 'isEmpty' was called on null.
Receiver: null
Tried calling: isEmpty
这是我的代码:
constants.dart
final usersRef = _fireStore.collection('users');
profile_screen.dart
FutureBuilder<DocumentSnapshot>(
future: usersRef.doc(widget.visitedUserId).get()
builder: (BuildContext context,AsyncSnapshot snapshot){
if(!snapshot.hasData){
print("Snapshot : ${usersRef.doc(widget.visitedUserId).get()}");
print("data : ${snapshot.data.data()}");
return Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(kTweeterColor),
),
);
}else{
UserModel userModel = UserModel.fromDoc(snapshot.data);
print("usermodel : ${userModel}");
return ListView(
physics: const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
children: [
Container(
height: 150,
decoration: BoxDecoration(
color: kTweeterColor,
image: userModel.coverImage.isEmpty
? null
: DecorationImage(fit: BoxFit.cover,
image: NetworkImage(userModel.coverImage)),
),
user_model.dart
import 'package:cloud_firestore/cloud_firestore.dart';
class UserModel {
String id;
String name;
String profilePicture;
String email;
String bio;
String coverImage;
UserModel(
{this.id,
this.name,
this.profilePicture,
this.email,
this.bio,
this.coverImage});
factory UserModel.fromDoc(DocumentSnapshot doc){
return UserModel(
id:doc.id,
name: doc.data()['name'],
email: doc.data()['email'],
profilePicture:doc.data()['profilePicture'],
bio: doc.data()['bio'],
coverImage: doc.data()['coverImage'],
);
}
}
其他问题 ***** 代码前
factory UserModel.fromDoc(DocumentSnapshot doc){
return UserModel(
String id;
String name;
String profilePicture;
String email;
String bio;
String coverImage;
id:doc.id,
name: doc.data()['name'],
email: doc.data()['email'],
profilePicture:doc.data()['profilePicture'],
bio: doc.data()['bio'],
coverImage: doc.data()['coverImage'],
之后******************
String id;
String name;
String profilePicture="";
String email;
String bio;
String coverImage ="";
factory UserModel.fromDoc(DocumentSnapshot doc){
return UserModel(
id:doc.id,
name: doc['name'],
email: doc['email'],
profilePicture:doc['profilePicture'],
bio: doc['bio'],
coverImage: doc['coverImage'],
);
}
***********Current Error****************
======== Exception caught by widgets library
=======================================================
Bad state: field does not exist within the DocumentSnapshotPlatform
The relevant error-causing widget was:
FutureBuilder<DocumentSnapshot>
file:///C:/src/flutterwork/flutter_twitter/lib/Screens/ProfileScreen.dart:64:15
I/chatty ( 1966): uid=10009(com.example.flutter_twitter) FirestoreWorker
identical 2 lines
W/flutter_twitte( 1966): Accessing hidden method Lsun/misc/Unsafe;-
>getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/flutter_twitte( 1966): Accessing hidden method
Ldalvik/system/CloseGuard;-
>close()V (greylist,core-platform-api, linking, allowed)
【问题讨论】:
-
尝试将
initialData设置为您的FutureBuilder
标签: android flutter dart google-cloud-firestore flutter-layout