Is there a way to access data from build method to initState method? because I have data that comes from firestore and I fetch it using futureBuilder but when data fetched whole UI updated. the problem now is I just want to get the variable from the build method called participantKeys to initialize it in the initState method, because I want to get the data from the futureBuilder, I tried a lot of solutions but always get the same (UI updated).
here is my methods
Future? getBusinessListFuture;
Future<ControllerModel?> getUserModelById(String uid) async {
ControllerModel? userModel;
try{
DocumentSnapshot docSnap = await FirebaseFirestore.instance.collection("users").doc(uid).get();
if(docSnap.data() != null) {
userModel = ControllerModel.fromMap(docSnap.data() as Map<String, dynamic>);
}
}catch(e){
print('$e (Firebase_helper)');
}
return userModel;
}
@override
void initState() {
streamBuilderChat = streamChat();
getBusinessListFuture = getUserModelById(' '); <=====
super.initState();
}
UI screen
StreamBuilder(
stream: streamBuilderChat,
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
if (snapshot.hasData) {
QuerySnapshot chatRoomSnapshot = snapshot.data as QuerySnapshot;
return Column(
children: [
ListView.builder(
physics: BouncingScrollPhysics(),
shrinkWrap: true,
itemCount: chatRoomSnapshot.docs.length,
itemBuilder: (BuildContext context, int index) {
ChatRoomModel chatRoomModel = ChatRoomModel.fromMap(
chatRoomSnapshot.docs[index].data()
as Map<String, dynamic>);
Map<String, dynamic> participants = chatRoomModel.participants!;
List<String> participantKeys = participants.keys.toList();
participantKeys.remove(widget.userModel.uid);
return FutureBuilder(
future:getBusinessListFuture,
// FirebaseHelper.getUserModelById(participantKeys[0]), /// <=====
builder: (context, userData) {
.....
Thanks.