Hi I have spent a couple of days on trying to figure out why my user looses there "session" on my app when I close the app. How do I keep them signed in or what might cause this?
I am running the app on my google pixel 5 using the simulator option on android studio.
yaml file:
firebase_core: ^0.4.0+9
firebase_auth: ^0.14.0+9
cloud_firestore: ^0.13.6
firebase_storage: 3.1.6
firebase_messaging: 6.0.16
firebase_crashlytics: 0.1.4+1
This is what I can see in my logs (Not helpful much):
D/FlutterLocationService(32338): Unbinding from location service. D/FlutterLocationService(32338): Destroying service. D/FlutterLocationService(32338): Creating service. D/FlutterLocationService(32338): Binding to location service.
flutter doctor -v:
(base) darrendupreez@Darrens-MacBook-Pro unavine_app % flutter doctor -v [ā] Flutter (Channel dev, 1.26.0-12.0.pre, on macOS 11.1 20C69 darwin-x64, locale en-CA) ā¢ Flutter version 1.26.0-12.0.pre at /Applications/flutter ā¢ Framework revision a706cd2112 (2 weeks ago), 2021-01-14 18:20:26 -0500 ā¢ Engine revision effb529ece ā¢ Dart version 2.12.0 (build 2.12.0-224.0.dev)
[ā] Android toolchain - develop for Android devices (Android SDK version 30.0.3) ā¢ Android SDK at /Users/darrendupreez/Library/Android/sdk ā¢ Platform android-30, build-tools 30.0.3 ā¢ Java binary at: /Users/darrendupreez/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/201.7042882/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java ā¢ Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) ā¢ All Android licenses accepted.
[ā] Xcode - develop for iOS and macOS ā¢ Xcode at /Applications/Xcode.app/Contents/Developer ā¢ Xcode 12.4, Build version 12D4e ā¢ CocoaPods version 1.10.1
[ā] Android Studio (version 4.1) ā¢ Android Studio at /Users/darrendupreez/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/201.7042882/Android Studio.app/Contents ā¢ Flutter plugin can be installed from: šØ https://plugins.jetbrains.com/plugin/9212-flutter ā¢ Dart plugin can be installed from: šØ https://plugins.jetbrains.com/plugin/6351-dart ā¢ Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
[ā] Connected device (1 available) ā¢ Pixel 5 (mobile) ā¢ 0C161FDD4000DT ā¢ android-arm64 ā¢ Android 11 (API 30)
ā¢ No issues found!
A code change has a visible effect only if the modified Dart code is run again after the change. Specifically, a hot reload causes all the existing widgets to rebuild. Only code involved in the rebuilding of the widgets is automatically re-executed. The main() and initState() functions, for example, are not run again.
Once your flutter project has been created do some changes in your code and perform a hot reload. In windows, you can perform a hot reload using 'ctrl+\' or using the hot reload button. In mac devices, you perform a hot reload using 'cmd+s'. If you are working in the command prompt using flutter run enter 'r' to run.
Close Android App With Code: exit(0) : This command also works but it is not recommended because it terminates the Dart VM process immediately and users may think that the app got crashed.
You can use shared_preferences: ^0.5.12+4
to save the session.Just check whether it is null or not. And when you need you can update the data with new login.
Sample:
SharedPreferences prefs = await SharedPreferences.getInstance(); int counter = (prefs.getInt('counter') ?? 0) + 1; print('Pressed $counter times.');
Providers and AuthCreditental should work here is an example for google login with firebase
main.dart
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return Provider(
create: (context) => AuthBloc(),
child: MaterialApp(
title: 'Your App',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: MaterialColor(0xff360A36, color),
accentColor: Colors.limeAccent,
),
//darkTheme: ThemeData.dark(),
home: LogInPage(),
),
);
}
}
auth_bloc.dart
class AuthBloc {
final authService = AuthService();
final googleSignin = GoogleSignIn(scopes: ['email']);
Stream<User> get currentUser => authService.currentUser;
loginGoogle() async {
try {
final GoogleSignInAccount googleUser = await googleSignin.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
idToken: googleAuth.idToken, accessToken: googleAuth.accessToken);
//Firebase Sign in
final result = await authService.signInWithCredential(credential);
print('${result.user.displayName}');
} catch (error) {
print(error);
}
}
logout() {
authService.logout();
googleSignin.signOut();
}
sign in button
SignInButton(
Buttons.Google,
text: "Sign in with Google",
onPressed: () => authBloc.loginGoogle(),
),
Also you can watch this tutorial for using credentials and providers
https://www.youtube.com/watch?v=_uYO2ht5Nl4&list=PL19w2HZFNl2BsftambzhFE7tVrqGEVwzy&index=1&t=1924s
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With