I am writting this flutter app using bloc/cubit architecture. I want to change user value but the function seems not working.
The print result I want is: 1-old user, 2-new user
Can you help me to understand why?
Here is my code
user favourite list
class FavouritesSrcreen extends StatelessWidget {
const FavouritesSrcreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocBuilder<LayoutCubit, LayoutStates>(builder: (context, state) {
var cubit = LayoutCubit.get(context);
return Scaffold(
body: ListView.separated(
...
return ListTile(
...
onTap: () {
print('1 ' + cubit.user.name);
cubit.getUserDataById(
cubit.users[index].uid,
);
print('2 ' + cubit.user.name);
cubit.changeIsFavourite();
});
},
));
});
}
}
cubit
...
void getUserDataById(String id) {
emit(LayoutGetUserLoadingState());
FirebaseFirestore.instance.collection('users').doc(id).get().then((value) {
user = UserData.fromJson(jsonDecode(jsonEncode(value.data())));
emit(LayoutGetOtherUserSuccessState());
}).catchError((error) {
print(error.toString());
emit(LayoutGetOtherUserErrorState(error.toString()));
});
}
...
You will need to call following method (FirebaseFirestore.instance.collection('users').doc(id).get()) in main thread using await and convert getUserDataById to future and call this method with await.