am developing a flutter application with firestore. in firestore I have a field called role and I managed to access it in my code. The role is printed correctly in the console.But I need help in displaying differnt page for each user.
`
class _UserMangmentState extends State<UserMangment> {
String role = "";
@override
void initState() {
getRole();
super.initState();
}
Future getRole() async {
String id = FirebaseAuth.instance.currentUser!.uid.toString();
FirebaseFirestore.instance
.collection('users')
.doc(id)
.get()
.then((DocumentSnapshot doc) {
role = doc.get('role');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Container();
} else {
getRole();
if (role == "vendor") {
return VendorInformation();
} else if (role == "planner") {
return PlannerPage();
} else {
return HomePage();
}
}
}),
);
}
}
`
2
Answers
You are trying to changing the role value without using setState the value of role never changed
Your approach isn’t optimal. You can try this. Hope it helps