I have a view in which a user can create an event, if the user creates the event, a value is saved via Shared Preferences that determines whether the user is an admin or not. If the user now goes to the view, the build widget should build different widget whether the user is a admin or guest. I have a functions thats checks if the user is an Admin or Guest. But the whole thing does not work properly yet and the correct widget is not built. Maybe i made a stupid mistake or maybe its not the best practice, i would appreciate every little help!
This is my build Method
@override
Widget build(BuildContext context) {
return checkifadmin() != null ? buildGuestView() : buildAdminView();
}
This is my Function that checks if its an Admin
Future<bool> checkifadmin() async {
final prefs = await SharedPreferences.getInstance();
final myevent = prefs.getString('event');
if (myevent == widget.eventName) {
print('Your are admin');
return true;
} else {
print('you are a guest');
return false;
}
}
The function works perfectly, it is always correctly recognized. The problem is likely to be with the build method
The BuildGuestView gets always build, when i change it to:
return checkifadmin() != null ? buildAdminView() : buildGuestView();
the buildadminview is always build.
You should rather use FutureBuilder
:
FutureBuilder<bool>(
future: checkIfAdmin(),
builder: (_, snapshot) {
final hasData = snapshot.data ?? false;
return hasData ? buildAdminView() : buildGuestView();
},
)