Firebase authenthication
Im trying to use firebase for my flutter apps, and its seem there is an error in my code , i know firebaseuser need to change to user, but its seem it does not work , please help me , im new.
import 'package:firebase_auth/firebase_auth.dart';
import 'package:quizmaker/view/signin.dart';
import 'package:quizmaker/models/user.dart';
class AuthService {
FirebaseAuth _auth = FirebaseAuth.instance;
Future signInEmailAndPass(String email, String password) async {
try {
UserCredential userCredential = await _auth.signInWithEmailAndPassword(
email: email, password: password);
User user = authResult.user; //the error is in this part
} catch (e) {
print(e.toString());
}
}
}
In this code snippet:
UserCredential userCredential = await _auth.signInWithEmailAndPassword(
email: email, password: password);
User user = authResult.user;
You're trying to use an authResult
variable that is never defined. My best guess is that you want to get the user from the userCredential
instead, so:
UserCredential userCredential = await _auth.signInWithEmailAndPassword(
email: email, password: password);
User user = userCredential.user;
This still won't work though, since userCredential.user
may be null, and is thus defined as User?
and not User
. Since you're using await
, you can be certain that there is a user, so you can do:
User user = userCredential.user!;