Navigator.pushReplacement 提供用于空值的空检查运算符。这次是 - 状态
Posted
技术标签:
【中文标题】Navigator.pushReplacement 提供用于空值的空检查运算符。这次是 - 状态【英文标题】:Navigator.pushReplacement gives Null check operator used on a null value. This time it is - state 【发布时间】:2022-01-16 04:56:55 【问题描述】:Google 登录提供程序类未导航到下一个屏幕。登录用户工作,但用户必须登录然后离开应用程序,然后重新打开。重新打开应用程序后,用户登录正常。
错误日志给出:
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value E/flutter ( 9884): #0 StatefulElement.state (package:flutter/src/widgets/framework.dart:4789:44) E/flutter ( 9884): #1 Navigator.of (package:flutter/src/widgets/navigator.dart:2730:47) E/flutter ( 9884): #2 GoogleSignInProvider.signInwithGoogle (package:blahblah/google_signin_provider.dart:37:17) E/flutter ( 9884): <asynchronous suspension>
错误指向 Navigator 类中的“状态”。如何设置它,以便应用导航?
class GoogleSignInProvider extends ChangeNotifier
final FirebaseAuth _auth = FirebaseAuth.instance;
User user = firebaseAuth.currentUser;
final GoogleSignIn _googleSignIn = GoogleSignIn();
Future<String> signInwithGoogle(BuildContext context) async
try
final GoogleSignInAccount googleSignInAccount =
await _googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
await _auth.signInWithCredential(credential);
on FirebaseAuthException catch (e)
print(e.message);
throw e;
final User currentUser = FirebaseAuth.instance.currentUser;
String uid = currentUser.uid;
if (currentUser != null)
await usersRef.doc(currentUser.uid).set('id': uid, 'email':
currentUser.email,
'username': currentUser.displayName, 'photoUrl':
currentUser.photoURL,
'phone': '', 'dob': '', 'sex': '');
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => TabScreen()));
notifyListeners();
return null;
Future logout() async
await _googleSignIn.disconnect();
FirebaseAuth.instance.signOut();
感谢您的帮助!
【问题讨论】:
【参考方案1】:由于您启用了可靠的 null 安全性,因此您无需对 currentUser 进行 null 检查。
【讨论】:
以上是关于Navigator.pushReplacement 提供用于空值的空检查运算符。这次是 - 状态的主要内容,如果未能解决你的问题,请参考以下文章