【发布时间】:2021-09-30 15:34:00
【问题描述】:
我在桌面上重新安装了 Flutter,更新了所有软件包和 Flutter 版本(当前运行 2.2.3)。由于某种原因,我的 StreamBuilder 不再工作。我收到以下错误:“NoSuchMethodError:在 null 上调用了 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 结果为 null。流的代码是:
//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映射快照不是问题。
我只是不明白为什么会这样。非常感谢任何输入。
【问题讨论】:
标签: flutter google-cloud-firestore snapshot stream-builder