我在消息屏幕上的listview.builder中显示firebase的所有用户,当我单击列表中的用户时,我想发送到聊天屏幕,该屏幕在应用程序栏中显示用户的个人资料图像和姓名
代码在此异常上暂停:
backgroundImage: widget.receiver.profileImageUrl.isEmpty
? AssetImage(
'assets/images/default_profile_picture.png')
: CachedNetworkImageProvider(
widget.receiver.profileImageUrl)),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
widget.receiver.name
相关列表视图:
body: ListView.builder(
itemCount: usersList.length,
itemBuilder: ((context, index) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: ((context) => ChatScreen(
profileImageUrl: usersList[index].profileImageUrl,
name: usersList[index].name,
receiverUid: usersList[index].uid,
// receiverUid: widget.currentUserId,
userId: widget.currentUserId,
receiver: user,
)
)
));
},
child: ListTile(
leading: CircleAvatar(
backgroundImage: usersList[index].profileImageUrl.isEmpty
? AssetImage(
'assets/images/default_profile_picture.png')
: CachedNetworkImageProvider(
usersList[index].profileImageUrl)),
title: Text(usersList[index].name),
),
),
);
}),
)
聊天屏幕构造函数:
class ChatScreen extends StatefulWidget {
final String profileImageUrl;
final String name;
final String receiverUid;
final User sender;
final User receiver;
final String userId;
ChatScreen(
{this.profileImageUrl,
this.name,
this.receiverUid,
this.sender,
this.receiver,
this.userId});
@override
_ChatScreenState createState() => _ChatScreenState();
}
聊天屏幕初始化状态:
@override
void initState() {
super.initState();
_repository.getCurrentUser().then((user) {
_currentUserId = user.uid;
setState(() {
sender = User(
id: user.uid,
name: user.displayName,
profileImageUrl: user.photoUrl);
});
});
_repository.getUserWithId(widget.userId).then((user) {
setState(() {
receiver = User(
id: user.id,
profileImageUrl: user.profileImageUrl,
name: user.name);
});
});
}
消息屏幕构造函数:
class MessagesScreen extends StatefulWidget {
final String userId;
final User receiver;
final String currentUserId;
final String receiverUid;
MessagesScreen(
{this.userId, this.receiver, this.currentUserId, this.receiverUid});
@override
_MessagesScreenState createState() => _MessagesScreenState();
}
答案 0 :(得分:1)
那是因为您的某些用户中的profileImageUrl为null。使用?在.isEmpty之前添加null检查。运算符。它将检查空值。
backgroundImage: (widget.receiver.profileImageUrl?.isEmpty ?? true)
? AssetImage(
'assets/images/default_profile_picture.png')
: CachedNetworkImageProvider(
widget.receiver.profileImageUrl)),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
widget.receiver.name