我在 Flutter 中使用 Firebase 作为后端,并且在用户身份验证中抛出异常。 每当我启动应用程序时,都会发生一次异常。
当我尝试注销时,出现一次异常但屏幕没有变化。如果我重新启动应用程序,屏幕会发生变化并且用户已注销,但现在会显示两次异常。如果我一直在此错误处重新启动应用程序,应用程序的其余部分就可以正常工作。
每当我尝试退出时,都会显示输出
D/FirebaseAuth(23343): Notifying id token listeners about a sign-out event.
D/FirebaseAuth(23343): Notifying auth state listeners about a sign-out event.
W/y_personal_che(23343): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
======== Exception caught by provider ==============================================================
The following assertion was thrown:
An exception was throw by _MapStream<User?, Userc> listened by
StreamProvider<Userc>, but no `catchError` was provided.
Exception:
NoSuchMethodError: The getter 'uid' was called on null.
Receiver: null
Tried calling: uid
====================================================================================================
这是我的验证码:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:my_personal_chef/Models/user.dart';
import 'package:my_personal_chef/services/database.dart';
class AuthService{
final FirebaseAuth _auth = FirebaseAuth.instance;
String uid;
//creating a user object from firebase user
Userc _userfromFirebaseUser (User user) {
print(user.uid);
return user!=null ? Userc(uid: user.uid, Email: user.email,) : null;
}
//auth change user stream
Stream <Userc> get user{
return _auth.authStateChanges()
.map((User user) => _userfromFirebaseUser(user));
}
//anonymous sign in
Future signinanon() async{
try{UserCredential result = await _auth.signInAnonymously();
final User user = result.user;
uid = user.uid;
print(uid);
await DatabaseService(userId: uid).addUser(_userfromFirebaseUser(user));
return _userfromFirebaseUser(user);
} catch(e){
print(e.toString());
return null;
}
}
//sign in with email and password
Future signinwithemailandpassword (String email, String password) async{
try{
UserCredential result = await _auth.signInWithEmailAndPassword(email: email, password: password);
final User user = result.user;
return _userfromFirebaseUser(user);
}
catch(e){
print(e.toString());
}
}
//register with email and password
Future registerwithemailandpassword(String email, String password)async{
try{
UserCredential result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
final User user = result.user;
uid = user.uid;
print(uid);
await DatabaseService(userId: uid).addUser(_userfromFirebaseUser(user));
return _userfromFirebaseUser(user);
}
catch(e){
print(e.toString());
return null;
}
}
String getUserbyId() {
return _auth.currentUser.uid;
}
//Sign out
Future signout() async{
try{
return await _auth.signOut();
}catch(e){
print(e.toString());
return null;
}
}
}
这是我的退出按钮 onPressed 属性:
onPressed: ()async{
await _auth.signout();
},
其中,最终的 AuthService _auth = AuthService();
这是我的项目文件。此外,欢迎任何贡献。 https://github.com/rajoriaakash/My-Personal-Chef
答案 0 :(得分:0)
Stream <Userc> get user{
return _auth.authStateChanges()
.map((User user) => _userfromFirebaseUser(user));
}
我认为问题出在这里,在某些版本的 firebase sdk 中,用户最初从 authStateChanges
变为 null。你最好先检查 null 并找到合适的版本,它最初不会为 null。
firebase_auth: ^1.1.1
这个版本很适合我。