InitState in flutter, deriving document value from firestore - firebase

I am working on an app where I need to filter out every user that signs in. Once they signed in, they will be redirected to wrapper that checks if the user ID exists on Firestore document collection. Here is my code.
class adminCheck extends StatefulWidget {
const adminCheck({Key? key}) : super(key: key);
#override
State<adminCheck> createState() => _adminCheckState();
}
class _adminCheckState extends State<adminCheck> {
User? user= FirebaseAuth.instance.currentUser;
bool isAdmin=false;
void initState() {
checkIfAdmin();
super.initState();
}
#override
Widget build(BuildContext context) {
if (isAdmin==true){
countDocuments();
return HomeScreen();
}else{
return notAdmin();
}
}
void checkIfAdmin() async{
DocumentSnapshot currentUser= await FirebaseFirestore.instance.collection('Administrator')
.doc(user!.email)
.get();
if(currentUser.exists){
print("This user is admin");
setState((){
isAdmin=true;
});
}
if(!currentUser.exists){
print("This user is not an admin");
}
}
}
The problem is it returns the notAdmin() class even the void method returns true which supposed to return HomeScreen(), and after few seconds, it will return HomeScreen(). I think there is a delay happening from initializing the data coming from the Firestore. Please help me with my problem. Or is there a way so that it will wait to be initialized first before returning any of those two classes?

The purpose of initState is to determine the state of the widget when it first renders. This means that you can't use asynchronous information (such as data that you still need to load from Firestore) in initState.
If the widget should only be rendered once the data is available, you should not render it until that data has been loaded. You can do this by adding another value to the state to indicate while the document is loading:
class _adminCheckState extends State<adminCheck> {
User? user= FirebaseAuth.instance.currentUser;
bool isAdmin=false;
bool isLoading=true; // 👈
And then use that in rendering:
#override
Widget build(BuildContext context) {
if (isLoading) { // 👈
return CircularProgressIndicator(); // 👈
} else if (isAdmin==true){
countDocuments();
return HomeScreen();
}else{
return notAdmin();
}
}
You can render whatever you want while the data is loading of course (or nothing at all), but the CircularProgressIndicator is typically a reasonable default.
And finally of course, you'll want to clear the loading indicator when the isAdmin is set:
void checkIfAdmin() async{
DocumentSnapshot currentUser= await FirebaseFirestore.instance.collection('Administrator')
.doc(user!.email)
.get();
if(currentUser.exists){
setState((){
isAdmin=true;
isLoading=false; // 👈
});
}
if(!currentUser.exists){
print("This user is not an admin");
}
}
This pattern is so common that it is also encapsulated in a pre-built widget. If that sounds appealing, you'll want to look at using a FutureBuilder.

Related

LateInitialization Error In Flutter main.dart

I am developing my app and I don't know why this late Initialization error has come I use this same code in my other apps as well there I don't face such error but in this main file the error is persisting and I have been trying for so long It doesn't works. bool? userLoggedIn isn't also working flutter doesn't letting it used. Here is my main.dart file code. Also If anyone can tell me how can I handle 2 logins of app shared preferences that would be a lot helpful
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late bool userLoggedIn;
#override
void initState() {
getLoggedInState();
super.initState();
}
getLoggedInState() async {
await HelperFunctions.getUserLoggedInSharedPreference().then((value) {
setState(() {
userLoggedIn = value!;
});
});
}
#override
Widget build(BuildContext context) {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Dashee',
theme: ThemeData(
primarySwatch: Colors.deepPurple,
),
home: userLoggedIn ? const Dashboard() : Splash());
}
}
LateInitializationError means a variable declared using the late keyword was not initialized on the constructor
You declare this boolean variable:
late bool userLoggedIn
but did not declare a constructor, so it won't be initialized, the obvious thing to do is giving it a value on the constructor, like so:
_MyAppState() {
userLoggedIn = false; // just some value so dart doesn't complain.
}
However may I suggest you don't do that and instead simply remove the late keyword?
Doing that, of course, will give you an error because userLoggedIn is never initialized, but you can fix that by giving it a default value straight on it's declaration or on the constructor initialization:
bool userLoggedIn = false;
or
_MyAppState(): userLoggedIn = false;
note how on the second option I didn't use the constructor's body, you should only declare a variable late if you plan on initializing it on the constructor's body.
This should solve the LateInitializationError that you are getting.
Regarding multiple log-ins
if you want to have three (or more!) log in states, I recommend you declare an enum of those states:
enum LogInState {
LoggedOff,
LoggedInAsRider,
LoggedInAsUser,
}
In order to store said state in sharedPreferences, you could store them as integers:
Future<void> savedLoggedInState(LogInState state) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setInt('some key', state.index);
}
then to read said value from shared preferences:
Future<LogInState> getLoginState() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
int index = prefs.getInt('some key') ?? 0;
return LogInState.values[index];
}
finally to display each different log in state, you'd do something like this:
home: _getLogInScreen(),
[...]
Widget _getLogInScreen() {
switch (userLogIn) {
case LogInState.LoggedOff:
return Splash();
case LogInState.LoggedInAsRider:
return RiderDashboard();
case LogInState.LoggedInAsUser:
return UserDashBoard();
}
// if you make a new log in state, you need to add it to the switch
// statement or it will throw an unimplemented error
throw UnimplementedError();
}

How to get the isAdmin field in FireStore and check if it's true or false in Flutter

Hello everyone I'm making an application with flutter which has different views for normal users admins and unauthenticated users, but the logic that I'm going for isn't working and I don't know why.
P.S here I'm only trying to differentiate between admins and normal users.
Here's what I'm trying to do:
Future<bool> isAdmin() async {
final currentUserUid = _firebaseAuth.currentUser.uid;
//What I'm trying to do here is get my isAdmin field which is created when a user is created
final DocumentSnapshot db = await
databaseReference.collection('users').doc(currentUserUid).get();
return db.data().containsKey('isAdmin');
}
This is how I'm trying to implement it
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../screens/auth/admin/admin_tab_bar_screen.dart';
import '../../screens/auth/user_tab_bar_screen.dart';
import '../../providers/auth.dart';
class AuthTabBarScreen extends StatelessWidget {
static const routeName = 'auth-tab-bar-view';
#override
Widget build(BuildContext context) {
final checkUserRole = Provider.of<Auth>(context, listen: false).isAdmin();
// Here I want to check the Value I'm returning if its true
if(checkUserRole == true){
return AdminTabBarScreen();
} else {
return UserTabBarScreen();
}
}
}
i didn't test it but this will probably work for you.
basically db.data() is a map so, if you search your key you will get what you want
like that
Future<bool> isAdmin() async {
final currentUserUid = _firebaseAuth.currentUser.uid;
final DocumentSnapshot db = await
databaseReference.collection('users').doc(currentUserUid).get();
return db.data()['isAdmin'];
}
You can use FutureBuilder which waits for a future (isAdmin() in your case) before building. snapshot.data is the value of the future.
class AuthTabBarScreen extends StatelessWidget {
static const routeName = 'auth-tab-bar-view';
#override
Widget build(BuildContext context) {
return FutureBuilder(
future: Provider.of<Auth>(context, listen: false).isAdmin(),
builder: (context, snapshot) => snapshot.hasData
? snapshot.data // if isAdmin() is true
? AdminTabBarScreen() // return Admin
: UserTabBarScreen() // else if false, return UserTab
: Loading(), // while you're waiting for the data, show some kind of loading indicator
);
}
}
isAdmin() returns Future<bool> which is a future value so when you compare Future<bool> and bool, you will receive an equality error because one is a Future and the other is a bool. Using FutureBuilder(), your app waits for Provider.of<Auth>...isAdmin() to return a value or snapshot. A snapshot is an async value of your future so calling snapshot.data is the same as await Provider.of<Auth>...isAdmin() which is why snapshot.data returns a bool and not a Future<bool>. It might be helpful to understand how futures, async, await work documentation.

Flutter: Problem with Firebase and Navigation

I use the next simple code for the whole process of login/register:
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return StreamProvider<FirebaseUser>.value(
value: _auth.onAuthStateChanged,
child: MaterialApp(
home: Wrapper(),
),
);
}
}
class Wrapper extends StatelessWidget {
#override
Widget build(BuildContext context) {
final firebaseuser = Provider.of<FirebaseUser>(context);
// return either Home or Authenticate widget:
if (firebaseuser == null) {
return WelcomeScreen();
} else {
return StreamProvider<MyUser>.value(
value: FirestoreService(uid: firebaseuser.uid).user,
child: MaterialApp(
home: HomeWrapper(),
),
);
}
}
}
As you can see I use a second StreamProvider for my User model (with user data) which is populated at the same time it is created or logged in:
#override
Future<MyUser> signUp(String email, String password, Extrainfo info) async {
try {
AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
FirebaseUser firebaseUser = result.user;
// create a new document for the user with the uid
FirestoreService().createUser(firebaseUser.uid,email,extrainfo);
return _userFromFirebaseUser(firebaseUser);
} catch (e) {
//print(e.toString());
//return e.toString();
}
}
Normally users register/log in the WelcomeScreen() and then the second stream (of MyUser) triggers the building of HomeWrapper() (from where I have all user's data available). Users get redirected here automatically.
The problem is: everything works fine unless I introduce navigation within the WelcomeScreen(). I need to have some screens within it, but once I do that, when registering the screen does not automatically change to HomeWrapper() (Although the value of the stream gets called). This does not happen when signing in since onAuthStateChanged gets called and the re-build is triggered higher up in the widget tree.
I guess the problem is that the second Provider is outside the scope of the navigation but I do not know how to fix this in a propper way.
in a proper way, i just suggest you to navigate to the home screen only when the user has been effectively registered; i mean in the SignUpScreen.
when you will call your signUp function: with then(/*navigation callback*/) instead of using a stream in your wrapper. just remove it, and try this way.

What is the cleanest way to access a logged in Firebase user across your Flutter app?

I am using firebase in my flutter app and accessing the logged in user would be done something like this -
Future<FirebaseUser> getLoggedInUser() async {
return await FirebaseAuth.instance.currentUser();
}
I use BLOC and was thinking of adding this to my AuthenticationBloc class. Since this BLOC is injected early, I should be able to access this from anywhere in the app -
AuthenticationBloc authBloc = BlocProvider.of<AuthenticationBloc>(context);
FirebaseUser user = await authBloc.getLoggedInUser()
But this leads to a few problems like accessing this in a StreamBuilder becomes an issue because of the need of using await which means I would need to make the StreamBuilder async (not sure how).
What would be the best/cleanest way of accessing the user object FirebaseUser user from anywhere in the app ?
Thanks !
Edit 1 :
So one way to do this would be to use a stateful widget every time I want the current user
class _MyWidgetState extends State<MyWidget> {
FirebaseUser user;
#override
void initState() {
super.initState();
_updateCurrentUser();
}
void _updateCurrentUser() async {
FirebaseUser currUser = await Firebase.instance.currentUser();
setState(() {
user = currUser;
});
}
But using a stateful widget every time I want the currUser seems weird. Is there a better way out ?
You can access the user by using stream such as
StreamBuilder(
stream:FirebaseAuth.instance.onAuthStateChanged,
builder:(context,snapshot){
if (snapshot.connectionState == ConnectionState.active) {
final user = snapshot.data;
return Text("User Id:${user.uid}");
}else{
return Center(
child:CircularProgressIndicator(),
);
}
}
);

Returning null user data from Firestore. How to reference it globaly instead?

I'm quite new to Flutter and I've been struggling to access a user's document on Firestore.
On the profile page,
I'm setting the current user's UID inside initState, but uid returns null for a quick second, then the page updates with correct info.
So I am able to retrieve a certain field (like displayName), but it isn't quite the best practice. I don't want to have a bunch of boilerplate code and await functions mixed with UI and such.
Code:
FirebaseUser user;
String error;
void setUser(FirebaseUser user) {
setState(() {
this.user = user;
this.error = null;
});
}
void setError(e) {
setState(() {
this.user = null;
this.error = e.toString();
});
}
#override
void initState() {
super.initState();
FirebaseAuth.instance.currentUser().then(setUser).catchError(setError);
}
Then in my body I have a Stream builder to get the document.
body: StreamBuilder(
stream: Firestore.instance
.collection('users')
.document(user.uid)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation<Color>(Colors.deepOrange),
),
);
} else {
var userDocument = snapshot.data;
return showProfileHeader(userDocument);
}
},
)
I want to make 'global' references to be accessed throughout the app. Instead of getting the user's id on every page and streaming a specific field when I might need multiple ones.
The only ways I found online to do something similar, created lists with all the data in it. I feel like this might get extra fields I don't need.
How can I make data from Firestore available across the app?
I am using the "Provider" package for doing state management across my app. Nowadays its also the suggested way by the google flutter team when it comes to state management. See the package here: https://pub.dev/packages/provider
Regarding Firebase Auth and accessing the credentials application wide, i am using that said package like stated on this page:
https://fireship.io/lessons/advanced-flutter-firebase/
Short version below. Bootstrap your app like so:
import 'package:provider/provider.dart';
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
// Make user stream available
StreamProvider<FirebaseUser>.value(
stream: FirebaseAuth.instance.onAuthStateChanged),
// not needed for your problem but here you can see how
// to define other Providers (types) for your app.
// You need a counter class which holds your model of course.
ChangeNotifierProvider(builder: (_) => Counter(0)),
],
// All data will be available in this child and descendents
child: MaterialApp(...)
);
}
}
Then in your child widgets, just do:
// Some widget deeply nested in the widget tree...
class SomeWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
var user = Provider.of<FirebaseUser>(context);
return Text(user.displayName) // or user.uid or user.email....
}
}
This should do the trick.
That happens because FirebaseAuth.instance.currentUser() returns a future, and until that future is completed, you will not have the proper FirebaseUser object.
Making the user object global is not a bad idea. In addition, you can hook it up to the FirebaseAuth stream so that it gets updated everytime the user auth status changes, like so in a user.dart file:
class User {
static FirebaseUser _user;
static get user => _user;
static void init() async {
_user = await FirebaseAuth.instance.currentUser();
FirebaseAuth.instance.onAuthStateChanged.listen((firebaseUser) {
_user = firebaseUser;
});
}
}
You can call User.init() in main() and access the user object with User.user.

Resources