我正在将streambuilder用于聊天应用程序。但是,一旦页面加载,我就会使用一个函数从Firebase检索用户详细信息。它可以正常工作,但是当streambuilder刷新时,它将返回到初始状态,并采用用户详细信息的初始值。 (代码说明如下):
String myName = "me"; ////Here is where I initialize the variables(ID AND NAME)
String id = '1';
Stream<QuerySnapshot> chats;
TextEditingController messageEditingController = TextEditingController();
bool isLoading = true;
Widget chatMessages() {
return StreamBuilder(
stream: chats,
builder: (context, snapshot) {
return snapshot.hasData
? ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
return MessageTile(
image: snapshot.data.documents[index].data["imgUrl"],
message: snapshot.data.documents[index].data["message"],
sendByMe:
myName == snapshot.data.documents[index].data["sendBy"], ////////This variable for name work fine when the screen loads but as soon as the stream refreshes, it takes the value from where it was initialized (Line 1 and 2). How do I get it to use only the value I get from the function?
);
})
: Center(
child: CupertinoActivityIndicator(
radius: 18,
));
},
);
}
@override
void initState() {
getUserDetails(); /////function called inside init
setState(() {
isLoading = false;
});
super.initState();
}
void getUserDetails() async { //////Here is where I fetch the user details.
UserModel user = await AuthService().getUser();
id = user.iD;
myName = user.name;
}