How to handle navigation after Firebase google login using provider? - flutter

I'm creating a simple app where user can be authenticated using firebase google_sigin. I have created AuthChecker widget which checks the authentication and returns login page, where user can login with google. After when the login in complete I want to go back to AuthChecker and show homepage.
Following code below is my implementation which gives context error
From the main.dart file the AuthChecker widget is called:
class AuthChecker extends StatefulWidget {
#override
_AuthCheckerState createState() => _AuthCheckerState();
}
class _AuthCheckerState extends State<AuthChecker> {
#override
Widget build(BuildContext context) {
return FutureBuilder(
future: getCurrentUser(),
builder: (context, snapshot){
if(snapshot.connectionState == ConnectionState.done){
//got response
FirebaseUser user = snapshot.data;
LoginStateProvider prov = Provider.of<LoginStateProvider>(context);
if(user == null){
//not loggedin pls login
return LoginPage();
}else{
//already logged in
print("Already logged in");
prov.updateUserState(user);
return Home();
}
}
},
);
}
}
The login page contains the signin button:
Widget signInButton(BuildContext context) {
return OutlineButton(
onPressed: () {
signInWithGoogle().whenComplete(() {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context)=>AuthChecker();
),
ModalRoute.withName('/auth_check')
);
},
}
The provider class looks like:
class LoginStateProvider with ChangeNotifier{
String uid;
String name;
String email;
String profilePhoto;
LoginStateProvider({
this.uid,
this.name,
this.email,
this.profilePhoto,
});
void updateUserState(FirebaseUser user){
this.uid = user.uid;
this.name = user.displayName;
this.email = user.email;
this.profilePhoto = user.photoUrl;
}
}
I got following error:
I/flutter ( 9551): * Ensure the Provider<LoginStateProvider> is an ancestor to this FutureBuilder<FirebaseUser>
I/flutter ( 9551): Widget
I/flutter ( 9551): * Provide types to Provider<LoginStateProvider>
I/flutter ( 9551): * Provide types to Consumer<LoginStateProvider>
I/flutter ( 9551): * Provide types to Provider.of<LoginStateProvider>()
I/flutter ( 9551): * Always use package imports. Ex: `import 'package:my_app/my_code.dart';
I/flutter ( 9551): * Ensure the correct `context` is being used.
I/flutter ( 9551):
I/flutter ( 9551): If none of these solutions work, please file a bug at:
I/flutter ( 9551): https://github.com/rrousselGit/provider/issues

you could try and do this:
Widget signInButton(BuildContext context) {
return OutlineButton(
onPressed: () {
signInWithGoogle().whenComplete(() {
FirebaseUser user = snapshot.data;
if (user == null) {
//Route to login
} else {
//route to somewhere
}
},
}
and create a splash screen with your logo and a async function that runs at initState that checks if user == null and route to login if needed or to homepage if already logged in.
there's a nice example here: Firebase Auth state check in Flutter
I'm not sure if it helps or if that was really your question, sorry if I misunderstood u

Related

state is always null in flutter_bloc

I am facing this weird issue, where though I have set the state, but the value when accessed in init state is showing null !!
user_cubit.dart
UserCubit() : super(UserInitialState()) {
emit(UserMainLoadingState());
_firestore
.collection("users")
.doc(_currentUser?.uid)
.snapshots()
.listen((event) {
event.exists
? {
emit(UserExists(
userModel: UserModel.fromjson(event.data()!, event.id)))
}
: {print("There is no such user"), emit(UserNotExists())};
});
}
user_state.dart
class UserState extends Equatable {
final UserModel? userModel;
const UserState({this.userModel});
#override
List<Object?> get props => [userModel];
}
class UserExists extends UserState {
UserExists({required UserModel userModel}) : super(userModel: userModel) { // Here the state is received
print("I am inside the UserExists state and the user is :$userModel");
}
}
myWidget.dart
#override
void initState() {
_userState = const UserState();
print("I am inside the initState The value of userstate is ${_userState.userModel}"); // This prints null , though i have set the state and console logs the state and it is confirmed that state exists why isn't this not null
if (_userState.userModel != null) {
print("user is ${_userState.userModel.toString()}");
}
super.initState();
}
Console log:
I/flutter ( 5029): I am inside the UserExists state and the user is :avatar boy
I/flutter ( 5029): fullName krrrt
I/flutter ( 5029): dob 19/1/2022
I/flutter ( 5029): email rd#xddf.co
I/flutter ( 5029): phone 12222222255
I/flutter ( 5029): I am inside the initState The value of userstate is null
Though the userState's userModel has value, why can't i access that in the `initState.
My tries :
I have used BlocListener, still the state remains null, I'm hell confused.
body: BlocListener<UserCubit, UserState>(
listener: (context, state) {
print("I am inside the listener"); // this line is never printed
if (state.userModel != null) {
print("State is ${state.runtimeType}"); // But if the state has some value, why isn't this being printed !!!??
}
},
child: <My widget>
It seems that the issue is that you are initializing the _userState variable in the initState method with const UserState(), which sets the userModel value to null. This is happening before the UserExists state is emitted from the UserCubit, which sets the userModel value to a valid UserModel object.
You should instead initialize _userState with the current state of the UserCubit by calling the state getter.
#override
void initState() {
_userCubit = context.bloc<UserCubit>();
_userState = _userCubit.state;
print("I am inside the initState The value of userstate is ${_userState.userModel}");
if (_userState.userModel != null) {
print("user is ${_userState.userModel.toString()}");
}
super.initState();
}
It would also be best practice to unsubscribe the listener after the widget is disposed, in the dispose method of your stateful widget.
#override
void dispose() {
_userCubit.close();
super.dispose();
}
Note that the above code assumes that you are using the bloc package to manage the state of your app.

How to access the state outside BlocConsumer and BlocCubit?

I am facing this weird issue, where though I have set the state, but the value when accessed in init state is showing null !!
user_cubit.dart
UserCubit() : super(UserInitialState()) {
emit(UserMainLoadingState());
_firestore
.collection("users")
.doc(_currentUser?.uid)
.snapshots()
.listen((event) {
event.exists
? {
emit(UserExists(
userModel: UserModel.fromjson(event.data()!, event.id)))
}
: {print("There is no such user"), emit(UserNotExists())};
});
}
user_state.dart
class UserState extends Equatable {
final UserModel? userModel;
const UserState({this.userModel});
#override
List<Object?> get props => [userModel];
}
class UserExists extends UserState {
UserExists({required UserModel userModel}) : super(userModel: userModel) {
print("I am inside the UserExists state and the user is :$userModel");
}
}
myWidget.dart
#override
void initState() {
_userState = const UserState();
print("I am inside the initState The value of userstate is ${_userState.userModel}"); // This prints null , but why ?
if (_userState.userModel != null) {
print("user is ${_userState.userModel.toString()}");
}
super.initState();
}
Console log:
I/flutter ( 5029): I am inside the UserExists state and the user is :avatar boy
I/flutter ( 5029): fullName krrrt
I/flutter ( 5029): dob 19/1/2022
I/flutter ( 5029): email rd#xddf.co
I/flutter ( 5029): phone 12222222255
I/flutter ( 5029): I am inside the initState The value of userstate is null
Though the userState's userModel has value, why can't i access that in the initState.
// Ignore this, this is for stackoverflow.
// Ignore this, this is for stackoverflow.
// Ignore this, this is for stackoverflow.
// Ignore this, this is for stackoverflow.
// Ignore this, this is for stackoverflow.
To access the state of a BLoC or Cubit in Flutter, you can use the context.read<Bloc>(). This method returns a Bloc instance that you can use to access the current state.
initState() {
super.initState();
_userState = const context.read<UserCubit>().state();
print("I am inside the initState The value of userstate is ${_userState.userModel}"); // This prints null , but why ?
if (_userState.userModel != null) {
print("user is ${_userState.userModel.toString()}");
}
}

Flutter | Riverpod & Dart Unhandled Exception: setState() or markNeedsBuild() called during build

Context
I have this AppUser class:
#immutable
class AppUser {
const AppUser({
this.displayName,
this.email,
required this.emailVerified,
this.phoneNumber,
this.photoURL,
required this.uid,
});
AppUser.fromFirebaseUser(User user)
: displayName = user.displayName,
email = user.email,
emailVerified = user.emailVerified,
phoneNumber = user.phoneNumber,
photoURL = user.photoURL,
uid = user.uid;
final String? displayName;
final String? email;
final bool emailVerified;
final String? phoneNumber;
final String? photoURL;
final String uid;
}
In order to manage and use the current user signed in, I have this AppUserController class:
class AppUserController extends StateNotifier<AppUser> {
AppUserController()
: super(
const AppUser(
emailVerified: false,
uid: '',
),
);
Stream<User?> get onAuthStateChanges =>
FirebaseAuth.instance.authStateChanges();
set setAppUser(AppUser appUser) {
state = appUser;
}
Future<void> signOut() async {
await FirebaseAuth.instance.signOut();
}
}
Then, I created 2 providers:
final appUserProvider =
StateNotifierProvider<AppUserController, AppUser>((ref) {
return AppUserController();
});
final appUserStreamProvider = StreamProvider<AppUser?>((ref) {
return ref
.read(appUserProvider.notifier)
.onAuthStateChanges
.map<AppUser?>((user) {
return user != null ? AppUser.fromFirebaseUser(user) : null;
});
});
I need to manage a user’s budgets list. Also, I have to synchronize this list with a Cloud Firestore database, so I created the BudgetsService class:
class BudgetsService {
BudgetsService({
required this.uid,
}) : budgetsRef = FirebaseFirestore.instance
.collection(FirestorePath.budgetsCollection(uid))
.withConverter<Budget>(
fromFirestore: (snapshot, _) => Budget.fromMap(snapshot.data()!),
toFirestore: (budget, _) => budget.toMap(),
);
String uid;
final CollectionReference<Budget> budgetsRef;
Future<void> addUpdate(Budget budget) async {
await budgetsRef.doc(documentPath(budget)).set(budget);
}
Future<void> delete(Budget budget) async {
await budgetsRef.doc(documentPath(budget)).delete();
}
String documentPath(Budget budget) => FirestorePath.budgetDoc(uid, budget);
Future<List<Budget>> getBudgets() async {
final list = await budgetsRef.get();
return list.docs.map((e) => e.data()).toList();
}
}
I use this class through budgetsServiceProvider provider:
final budgetsServiceProvider = Provider<BudgetsService>((ref) {
final AppUser appUser = ref.watch(appUserProvider);
final String uid = appUser.uid;
return BudgetsService(uid: uid);
});
I use BudgetsService class only to interact with the online database. For the rest, I manage the user’s budget list with BudgetsController class:
class BudgetsController extends StateNotifier<List<Budget>> {
BudgetsController() : super(<Budget>[]);
List<String> get names => state.map((b) => b.name).toList();
Future<void> addUpdate(Budget budget, BudgetsService budgetsService) async {
await budgetsService.addUpdate(budget);
if (budgetAlreadyExists(budget)) {
final int index = indexOf(budget);
final List<Budget> newState = [...state];
newState[index] = budget;
state = newState..sort();
} else {
state = [...state, budget]..sort();
}
}
bool budgetAlreadyExists(Budget budget) => names.contains(budget.name);
Future<void> delete(Budget budget, BudgetsService budgetsService) async {
await budgetsService.delete(budget);
final int index = indexOf(budget);
if (index != -1) {
final List<Budget> newState = [...state]
..removeAt(index)
..sort();
state = newState;
}
}
Future<void> retrieveBudgets(BudgetsService budgetsService) async {
state = await budgetsService.getBudgets();
}
int indexOf(Budget budget) => state.indexWhere((b) => b.name == budget.name);
}
I use this class through budgetsProvider provider:
final budgetsProvider =
StateNotifierProvider<BudgetsController, List<Budget>>((ref) {
return BudgetsController();
});
After the user is signed in, my SwitchScreen widget navigates to ConsoleScreen:
class SwitchScreen extends HookWidget {
const SwitchScreen({
Key? key,
}) : super(key: key);
static const route = '/switch';
#override
Widget build(BuildContext context) {
final appUserStream =
useProvider<AsyncValue<AppUser?>>(appUserStreamProvider);
final googleSignIn =
useProvider<GoogleSignInService>(googleSignInServiceProvider);
final appUserController =
useProvider<AppUserController>(appUserProvider.notifier);
return appUserStream.when(
data: (data) {
if (data != null) {
appUserController.setAppUser = data;
final budgetsService = useProvider(budgetsServiceProvider);
return const ConsoleScreen();
} else {
return SignInScreen(
onGooglePressed: googleSignIn.signInWithGoogle,
);
}
},
loading: () {
return const Scaffold(
body: Center(
child: LinearProgressIndicator(),
),
);
},
error: (error, stack) {
return Scaffold(
body: Center(
child: Text('Error: $error'),
),
);
},
);
}
}
Problem
The first time I build the app, I have no problem. But when I perform the hot reload, I get the following error message:
══════ Exception caught by widgets library ═══════════════════════════════════
The following Error was thrown building SwitchScreen(dirty, dependencies: [UncontrolledProviderScope], AsyncValue<AppUser?>.data(value: Instance of 'AppUser'), Instance of 'GoogleSignInService', Instance of 'AppUserController'):
Instance of 'Error'
The relevant error-causing widget was
SwitchScreen
lib\main.dart:67
When the exception was thrown, this was the stack
#0 StateNotifier.state=
package:state_notifier/state_notifier.dart:173
#1 AppUserController.setAppUser=
package:financesmanager/controllers/app_user_controller.dart:42
#2 SwitchScreen.build.<anonymous closure>
package:financesmanager/screens/switch_screen.dart:33
#3 _$AsyncData.when
package:riverpod/src/common.freezed.dart:148
#4 SwitchScreen.build
package:financesmanager/screens/switch_screen.dart:28
...
════════════════════════════════════════════════════════════════════════════════
E/flutter (13932): [ERROR:flutter/shell/common/shell.cc(103)] Dart Unhandled Exception: setState() or markNeedsBuild() called during build.
E/flutter (13932): This UncontrolledProviderScope widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
E/flutter (13932): The widget on which setState() or markNeedsBuild() was called was:
E/flutter (13932): UncontrolledProviderScope
E/flutter (13932): The widget which was currently being built when the offending call was made was:
E/flutter (13932): SwitchScreen, stack trace: #0 Element.markNeedsBuild.<anonymous closure>
package:flutter/…/widgets/framework.dart:4217
E/flutter (13932): #1 Element.markNeedsBuild
package:flutter/…/widgets/framework.dart:4232
E/flutter (13932): #2 ProviderElement._debugMarkWillChange.<anonymous closure>
package:riverpod/…/framework/base_provider.dart:660
E/flutter (13932): #3 ProviderElement._debugMarkWillChange
package:riverpod/…/framework/base_provider.dart:664
E/flutter (13932): #4 ProviderStateBase.exposedValue=.<anonymous closure>
package:riverpod/…/framework/base_provider.dart:900
E/flutter (13932): #5 ProviderStateBase.exposedValue=
package:riverpod/…/framework/base_provider.dart:902
E/flutter (13932): #6 _StateNotifierProviderState._listener
package:riverpod/src/state_notifier_provider.dart:92
E/flutter (13932): #7 StateNotifier.state=
package:state_notifier/state_notifier.dart:162
E/flutter (13932): #8 AppUserController.setAppUser=
package:financesmanager/controllers/app_user_controller.dart:42
E/flutter (13932): #9 SwitchScreen.build.<anonymous closure>
package:financesmanager/screens/switch_screen.dart:33
Question
How can I solve the problem?
Thank you very much!
Update (2021-06-08)
In my main.dart file I have:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
runApp(ProviderScope(child: FMApp()));
}
class FMApp extends HookWidget {
FMApp({
Key? key,
}) : super(key: key);
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
#override
Widget build(BuildContext context) {
final darkTheme = AppTheme.theme(Brightness.dark);
final lightTheme = AppTheme.theme(Brightness.light);
final isLightTheme = useProvider<bool>(themePreferenceProvider);
final theme = isLightTheme ? lightTheme : darkTheme;
return FutureBuilder(
future: _initialization,
builder: (context, snapshot) {
if (snapshot.hasError) {
return FlutterFireInitErrorScreen(
appTitle: 'FM App',
darkTheme: darkTheme,
error: snapshot.error,
theme: theme,
);
}
if (snapshot.connectionState == ConnectionState.done) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'FM App',
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale.fromSubtags(languageCode: 'en'),
Locale.fromSubtags(languageCode: 'es'),
Locale.fromSubtags(languageCode: 'it'),
],
darkTheme: darkTheme,
theme: theme,
initialRoute: SwitchScreen.route,
routes: {
SwitchScreen.route: (context) => const SwitchScreen(),
},
);
}
return FlutterFireInitWaitingScreen(
appTitle: 'FM App',
darkTheme: darkTheme,
theme: theme,
);
},
);
}
}
Possible solution
For now I solved it by replacing, in switch_screen.dart file, this code:
final budgetsService = useProvider(budgetsServiceProvider);
final budgetsController = context.read<BudgetsController>(budgetsProvider.notifier);
budgetsController.retrieveBudgets(budgetsService);
with the following:
final budgetsService = BudgetsService(uid: data.uid);
context
.read(budgetsControllerProvider)
.retrieveBudgets(budgetsService);
What do you think? Is this a good solution? Is there a better one? Thank you!
The interpretation of the error is that two widgets are updating at the same time, probably because they watch the same provider.
When a Child Widget tries to rebuild while its Parent Widget also tries to rebuild, it generates this error. To solve this error, only the Parent Widget needs to rebuild, because the Child Widget will automatically rebuild.
Unfortunately, in the code you provide, I cannot see from where your SwitchScreen is displayed so I cannot tell you where the exact problem could be.

Flutter: StreamProvider<FirebaseUser> with onAuthStateChanged always returns null as a first value

I'm trying to create a simple navigation according to auth state to Firebase, but for some reason
Provider.of<FirebaseUser>(context) always returns null as a first value in the widget build method. So my app always navigates to auth screen and after that gets the real user status from Firebase.
What am I missing here?
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) => runApp(App()));
}
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return StreamProvider<FirebaseUser>.value(
//value of user always corresponds to the right auth status
value: FirebaseAuth.instance.onAuthStateChanged.map((FirebaseUser user) {
AuthService.instance.user = user;
return user;
}),
child: MaterialApp(
title: 'Title',
theme: lightTheme,
home: LandingScreen(),
),
);
}
}
class LandingScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
//Even though the user is logged in it returns null for the first build() call
final user = Provider.of<FirebaseUser>(context);
return AnimatedSwitcher(
duration: const Duration(milliseconds: 3000),
transitionBuilder: ...,
child: user != null ? const MainScreen() : const AuthScreen(),
);
}
}
You can subscribe to FirebaseAuth authStateChanges() stream to listen to the user's current authentication state.
FirebaseAuth.instance
.authStateChanges()
.listen((User? user) {
if (user == null) {
print('User is currently signed out!');
} else {
print('User is signed in!');
}
}
);
But checking the code you've provided, auth state is being fetched on final user = Provider.of<FirebaseUser>(context);. If the provider wasn't updated upon user login, then it will always be null. You may want to use FirebaseAuth.instance.currentUser() instead.

Problem using Flutter Provider when updating a value during build

I'm trying to update my uid in a provider just after checking whether a user is logged. When I do that, throws an error when building widgets even though the app does not crash. Here is the code:
class HandleAuth extends StatelessWidget {
#override
Widget build(BuildContext context) {
var user = Provider.of<FirebaseUser>(context);
if (user != null) {
print('user.uid is ${user.uid}');
final loggedUserInfo = Provider.of<LoggedUserInfo>(context, listen: false);
loggedUserInfo.updateUserInfo(user.uid);
print('first scan screen user: ${loggedUserInfo.userUid}');
}
return (user == null)
? WelcomeNewUserScreen()
: ServicesAroundMe();
}
}
And here is the provider:
class LoggedUserInfo with ChangeNotifier {
String _uid;
String get userUid {
return _uid;
}
void updateUserInfo(String updatedUid) {
_uid = updatedUid;
print('updated uid is $_uid');
notifyListeners();
}
}
It throws this error:
This ListenableProvider widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: ListenableProvider
You have to bear in mind that each time you call the method updateUserInfo, the notifyListeners() is triggered which tries to rebuild dirty widgets. Provider.of<T>(context) without the argument listen: false also does the same thing. Hence 2 rebuild trigger are called which cause the error.
When working with a provider, it is advisable to use a stream.
To make your code more scalable, create a custom class for your user and Either use ChangeNotifier or provider and streams.
For example;
class User {
final String uid;
final String displayName;
User({ #required this.uid, this.displayName });
}
class Auth {
User _firebaseUserMapper( FirebaseUser user) {
if(user == null){
return null;
}
return User(uid: user.uid, displayName: user.displayName);
}
Stream<User> get onAuthStateChange {
return Firebase.instance.onAuthStateChanged.map(_firebaseUserMapper);
}
}
In your page screen, you use like bellow
class HandleAuth extends StatelessWidget {
#override
Widget build(BuildContext context) {
final auth = Provider.of<Auth>(context, listen: false);
return StreamBuilder<User>(
stream: auth.onAuthStateChanged,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if( snapshot.connectionState == ConnectionState.active) {
User user = snapshot.data;
if (user == null ){
return WelcomeNewUserScreen();
}
return Provider<User>.value(
value: user,
child: ServicesAroundMe(),
);
}
return Scaffold(
body: Center(
child: CircularProgressIndicator();
),
);
}
);
}
The stream will forever listen for a currentUser/newUser and navigate to the appropriate page.