例外:NoSuchMethodError:在 null 上调用了 getter 'uid'。接收方:null 尝试调用:uid
Posted
技术标签:
【中文标题】例外:NoSuchMethodError:在 null 上调用了 getter \'uid\'。接收方:null 尝试调用:uid【英文标题】:Exception: NoSuchMethodError: The getter 'uid' was called on null. Receiver: null Tried calling: uid例外:NoSuchMethodError:在 null 上调用了 getter 'uid'。接收方:null 尝试调用:uid 【发布时间】:2021-07-27 10:02:07 【问题描述】:我在 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
tryUserCredential 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;
这是我的 SignOut 按钮 onPressed 属性:
onPressed: ()async
await _auth.signout();
,
其中,最终的 AuthService _auth = AuthService();
这是我的项目文件。此外,欢迎任何贡献。 https://github.com/rajoriaakash/My-Personal-Chef
【问题讨论】:
这里是 ` return user!=null ? Userc(uid: user.uid, Email: user.email,) : null;` from_userfromFirebaseUser
你正在映射到 null
这只是一个将 Firebase 用户数据转换为我的自定义用户模型数据的函数。我如何在这里映射到 null?
【参考方案1】:
Stream <Userc> get user
return _auth.authStateChanges()
.map((User user) => _userfromFirebaseUser(user));
我认为问题出在这里,在某些版本的 firebase sdk 中,用户最初来自 authStateChanges
。您最好检查一下 null 并找到最初不为 null 的正确版本。
firebase_auth: ^1.1.1
这个版本适合我。
【讨论】:
以上是关于例外:NoSuchMethodError:在 null 上调用了 getter 'uid'。接收方:null 尝试调用:uid的主要内容,如果未能解决你的问题,请参考以下文章
NoSuchMethodError (NoSuchMethodError: 方法 'map' 在 null 上被调用
NoSuchMethodError(NoSuchMethodError:方法'[]'在null上被调用。接收者:null尝试调用:[](“title”))