I’m creating an app in flutter. There is a matter that happens when I attempt to check in utilizing firebase authentication (by e-mail and password). The error that’s occuring is:
Exception has occurred.
_CastError (Null examine operator used on a null worth)
I’ve no clue why my code impulsively stopped working, and no concept the place this error even is happening. I’ve pinpointed the place in my code the difficulty is, however the factor is I’m referencing firebase’s authentication service and that’s the place the difficulty lies. Right here is the problematic perform:
Future<Consumer?> signInWithEmailPassword(String e-mail, String password) async {
await Firebase.initializeApp();
Consumer? consumer;
attempt {
UserCredential userCredential = await _auth.signInWithEmailAndPassword(
e-mail: e-mail,
password: password,
);
consumer = userCredential.consumer;
if (consumer != null) {
uid = consumer.uid;
userEmail = consumer.e-mail;
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setBool('auth', true);
// prefs.setString('id', consumer.uid);
}
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
print('No consumer discovered for that e-mail.');
} else if (e.code == 'wrong-password') {
print('Unsuitable password supplied.');
}
}
return consumer;
}
ISSUE: Within the first line of the attempt block, there may be an await on the _auth perform. This throws the error above. Right here is the complete code of the whole authentication program:
import 'package deal:firebase_auth/firebase_auth.dart';
import 'package deal:firebase_core/firebase_core.dart';
import 'package deal:google_sign_in/google_sign_in.dart';
import 'package deal:shared_preferences/shared_preferences.dart';
import 'package deal:flutter/basis.dart' present kIsWeb;
remaining FirebaseAuth _auth = FirebaseAuth.occasion;
remaining GoogleSignIn googleSignIn = GoogleSignIn();
String? uid;
String? identify;
String? userEmail;
String? imageUrl;
/// For checking if the consumer is already signed into the
/// app utilizing Google Signal In
Future getUser() async {
await Firebase.initializeApp();
SharedPreferences prefs = await SharedPreferences.getInstance();
bool? authSignedIn = prefs.getBool('auth');
print(authSignedIn);
remaining Consumer? consumer = _auth.currentUser;
if (authSignedIn == true) {
if (consumer != null) {
uid = consumer.uid;
identify = consumer.displayName;
userEmail = consumer.e-mail;
imageUrl = consumer.photoURL;
prefs.setString('identify', identify!);
prefs.setString('id', uid!);
}
}
}
/// For authenticating consumer utilizing Google Signal In
/// with Firebase Authentication API.
///
/// Retrieves some common consumer associated info
/// from their Google account for ease of the login course of
Future<Consumer?> signInWithGoogle() async {
await Firebase.initializeApp();
Consumer? consumer;
if (kIsWeb) {
GoogleAuthProvider authProvider = GoogleAuthProvider();
attempt {
remaining UserCredential userCredential =
await _auth.signInWithPopup(authProvider);
consumer = userCredential.consumer;
} catch (e) {
print(e);
}
} else {
remaining GoogleSignIn googleSignIn = GoogleSignIn();
remaining GoogleSignInAccount? googleSignInAccount =
await googleSignIn.signIn().catchError((onError) => print(onError));;
if (googleSignInAccount != null) {
remaining GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
remaining AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
attempt {
remaining UserCredential userCredential =
await _auth.signInWithCredential(credential);
consumer = userCredential.consumer;
} on FirebaseAuthException catch (e) {
if (e.code == 'account-exists-with-different-credential') {
print('The account already exists with a special credential.');
} else if (e.code == 'invalid-credential') {
print('Error occurred whereas accessing credentials. Strive once more.');
}
} catch (e) {
print(e);
}
}
}
if (consumer != null) {
uid = consumer.uid;
identify = consumer.displayName;
userEmail = consumer.e-mail;
imageUrl = consumer.photoURL;
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('auth', true);
//prefs.setString('id', consumer.uid);
//prefs.setString('identify', consumer.displayName!);
}
return consumer;
}
Future resetPassword(String e-mail) async {
attempt {
print(e-mail);
await FirebaseAuth.occasion.sendPasswordResetEmail(e-mail: e-mail);
} on FirebaseAuthException catch(e) {
print(e);
return false;
}
return true;
}
Future<Consumer?> registerWithEmailPassword(String e-mail, String password) async {
await Firebase.initializeApp();
Consumer? consumer;
attempt {
UserCredential userCredential = await _auth.createUserWithEmailAndPassword(
e-mail: e-mail,
password: password,
);
consumer = userCredential.consumer;
if (consumer != null) {
uid = consumer.uid;
userEmail = consumer.e-mail;
//SharedPreferences prefs = await SharedPreferences.getInstance();
//prefs.setString('id', consumer.uid);
//prefs.setString('identify', consumer.displayName!);
}
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password supplied is simply too weak.');
} else if (e.code == 'email-already-in-use') {
print('The account already exists for that e-mail.');
}
} catch (e) {
print(e);
}
return consumer;
}
Future<Consumer?> signInWithEmailPassword(String e-mail, String password) async {
await Firebase.initializeApp();
Consumer? consumer;
attempt {
UserCredential userCredential = await _auth.signInWithEmailAndPassword(
e-mail: e-mail,
password: password,
);
consumer = userCredential.consumer;
if (consumer != null) {
uid = consumer.uid;
userEmail = consumer.e-mail;
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setBool('auth', true);
// prefs.setString('id', consumer.uid);
}
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
print('No consumer discovered for that e-mail.');
} else if (e.code == 'wrong-password') {
print('Unsuitable password supplied.');
}
}
return consumer;
}
Future<String> signOut() async {
await _auth.signOut();
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('auth', false);
uid = null;
userEmail = null;
return 'Consumer signed out';
}
/// For signing out of their Google account
void signOutGoogle() async {
await googleSignIn.signOut();
await _auth.signOut();
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('auth', false);
uid = null;
identify = null;
userEmail = null;
imageUrl = null;
print("Consumer signed out of Google account");
}
Thanks a lot! This problem is tremendous annoying and I’ve been coping with it for 3 days now.