Streambuilder 快照为空且有错误
Streambuilder snapshot is null and has error
我在桌面上重新安装了 flutter,更新了所有软件包和 flutter 版本(当前 运行 2.2.3)。出于某种原因,我的 StreamBuilder 不再有效。我收到以下错误:“NoSuchMethodError:getter 'isEmpty' 被调用为空。”
这是我的小部件的代码:
Container(
color: Theme.of(context).scaffoldBackgroundColor,
child: Column(
children: <Widget>[
StreamBuilder<List<User>>(
initialData: [],
stream:
DatabaseService().getChattingWith(user.uid),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Loading();
default:
List<User> userList = snapshot.data;
if (snapshot.hasError) {
print("something went wrong");
}
if (!snapshot.hasData)return Text("No data");
print(userList);
return userList.isEmpty
? Center(
child: Container(
padding: EdgeInsets.fromLTRB(
5, 100, 5, 10),
child: Column(
mainAxisAlignment:
MainAxisAlignment.start,
children: [
Text("No Chats",
style: Theme.of(context)
.textTheme
.headline4),
SizedBox(
height: 30,
),
Container(
width: 180.0,
height: 180.0,
decoration:
new BoxDecoration(
shape:
BoxShape.circle,
border: Border.all(
color: Theme.of(
context)
.accentColor,
width: 4,
),
image:
new DecorationImage(
image: new ExactAssetImage(
'assets/splashscreen.png'),
fit: BoxFit.cover,
),
)),
SizedBox(
height: 40,
),
Align(
alignment:
Alignment.center,
child: ArrowElement(
id: 'arrow',
show: true,
targetId: "profile",
tipLength: 20,
width: 5,
bow: 0.31,
padStart: 30,
padEnd: 43,
arcDirection:
ArcDirection.Right,
sourceAnchor:
Alignment.center,
targetAnchor:
Alignment(0, 0),
color: Theme.of(context)
.accentColor,
child: Column(
children: [
Text(
"Search to find matches",
style: Theme.of(
context)
.textTheme
.headline5),
],
),
),
),
],
),
),
)
: ListView.builder(
physics: BouncingScrollPhysics(),
itemCount: userList.length,
itemBuilder: (context, index) {
return UserTile(
currentUser: user,
user: userList[index],
);
},
);
}
}),
],
),
),
当 运行 执行“出错了”的代码时,打印 userList 结果为空。 Stream 的代码是:
//Userlist from snapshot
List<User> _userListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((doc) {
return User(
uid: doc['uid'],
radius: doc['radius'] ?? 1,
searching: doc['searching'] ?? false,
searchTime: doc['searchTime'],
timestamp: doc['timestamp'],
displayName: doc['displayName'],
email: doc['email']);
}).toList();
}
//Get chattingWith stream
Stream<List<User>> getChattingWith(String uid) {
return FirebaseFirestore.instance
.collection('users/$uid/chattingWith')
.snapshots()
.map(_userListFromSnapshot);
}
chattingWith 集合仅包含 documentID、isSeen(布尔值)和 matchTime(时间戳)。但是以前用_userListFromSnapshot映射快照没有问题。
我根本不明白为什么会这样。非常感谢任何输入。
流可以处于更多状态,但您只处理 ConnectionState.waiting
和“其他所有内容”。
如果你保持你的代码不变,你应该在访问它的数据之前检查快照是否有数据,假设它存在:
if (!snapshot.hasData) return Text("No data");
return userList.isEmpty
...
我在桌面上重新安装了 flutter,更新了所有软件包和 flutter 版本(当前 运行 2.2.3)。出于某种原因,我的 StreamBuilder 不再有效。我收到以下错误:“NoSuchMethodError:getter 'isEmpty' 被调用为空。”
这是我的小部件的代码:
Container(
color: Theme.of(context).scaffoldBackgroundColor,
child: Column(
children: <Widget>[
StreamBuilder<List<User>>(
initialData: [],
stream:
DatabaseService().getChattingWith(user.uid),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Loading();
default:
List<User> userList = snapshot.data;
if (snapshot.hasError) {
print("something went wrong");
}
if (!snapshot.hasData)return Text("No data");
print(userList);
return userList.isEmpty
? Center(
child: Container(
padding: EdgeInsets.fromLTRB(
5, 100, 5, 10),
child: Column(
mainAxisAlignment:
MainAxisAlignment.start,
children: [
Text("No Chats",
style: Theme.of(context)
.textTheme
.headline4),
SizedBox(
height: 30,
),
Container(
width: 180.0,
height: 180.0,
decoration:
new BoxDecoration(
shape:
BoxShape.circle,
border: Border.all(
color: Theme.of(
context)
.accentColor,
width: 4,
),
image:
new DecorationImage(
image: new ExactAssetImage(
'assets/splashscreen.png'),
fit: BoxFit.cover,
),
)),
SizedBox(
height: 40,
),
Align(
alignment:
Alignment.center,
child: ArrowElement(
id: 'arrow',
show: true,
targetId: "profile",
tipLength: 20,
width: 5,
bow: 0.31,
padStart: 30,
padEnd: 43,
arcDirection:
ArcDirection.Right,
sourceAnchor:
Alignment.center,
targetAnchor:
Alignment(0, 0),
color: Theme.of(context)
.accentColor,
child: Column(
children: [
Text(
"Search to find matches",
style: Theme.of(
context)
.textTheme
.headline5),
],
),
),
),
],
),
),
)
: ListView.builder(
physics: BouncingScrollPhysics(),
itemCount: userList.length,
itemBuilder: (context, index) {
return UserTile(
currentUser: user,
user: userList[index],
);
},
);
}
}),
],
),
),
当 运行 执行“出错了”的代码时,打印 userList 结果为空。 Stream 的代码是:
//Userlist from snapshot
List<User> _userListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((doc) {
return User(
uid: doc['uid'],
radius: doc['radius'] ?? 1,
searching: doc['searching'] ?? false,
searchTime: doc['searchTime'],
timestamp: doc['timestamp'],
displayName: doc['displayName'],
email: doc['email']);
}).toList();
}
//Get chattingWith stream
Stream<List<User>> getChattingWith(String uid) {
return FirebaseFirestore.instance
.collection('users/$uid/chattingWith')
.snapshots()
.map(_userListFromSnapshot);
}
chattingWith 集合仅包含 documentID、isSeen(布尔值)和 matchTime(时间戳)。但是以前用_userListFromSnapshot映射快照没有问题。
我根本不明白为什么会这样。非常感谢任何输入。
流可以处于更多状态,但您只处理 ConnectionState.waiting
和“其他所有内容”。
如果你保持你的代码不变,你应该在访问它的数据之前检查快照是否有数据,假设它存在:
if (!snapshot.hasData) return Text("No data");
return userList.isEmpty
...