Flutter GetX 包 Firebase Auth 和 FireStore "null check operator used on a null value"
Flutter GetX package Firebase Auth and FireStore "null check operator used on a null value"
首先,我是第一次提问,如果我问的不正确和不完整,我会立即给你你想要的信息。
嘿,我正在按照本视频中的教程进行操作,但出现了这样的错误。
视频教程:https://www.youtube.com/watch?v=BiV0DcXgk58&t=314s&ab_channel=TadasPetra
错误是这样的:
Error simulator picture
我收到此错误,但帐户是在 firebase Auth 中创建的,我想要的数据保存在 FireStore 数据库中。
颤振版本:2.2.3
pubspec.yaml
cloud_firestore: ^0.13.5
firebase_core: ^0.4.5
firebase_storage: ^3.1.6
get: ^4.3.8
错误调试控制台文本
[GETX] Instance "AuthController" has been created
[GETX] Instance "AuthController" has been initialized
[GETX] Instance "GetMaterialController" has been created
[GETX] Instance "GetMaterialController" has been initialized
════════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown building Root:
Null check operator used on a null value
The relevant error-causing widget was
Root
lib/main.dart:20
When the exception was thrown, this was the stack
#0 GetXState.initState
package:get/…/rx_flutter/rx_getx_widget.dart:78
#1 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4711
#2 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4548
#3 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3611
#4 Element.updateChild
package:flutter/…/widgets/framework.dart:3363
所有文件
Main.dart代码
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
initialBinding: AuthBinding(),
theme: ThemeData(scaffoldBackgroundColor: bgColor),
home: Root(),
);
}
}
Root.dart代码
class Root extends GetWidget<AuthController> {
@override
Widget build(BuildContext context) {
return GetX(
initState: (_) async {
Get.put<UserController>(UserController());
},
builder: (_) {
if (Get.find().user?.uid != null) {
return SMBottomNavBar();
} else {
return SignInScreen();
}
},
);
}
}
userController.dart
class UserController extends GetxController {
Rx<UserModel> _userModel = UserModel().obs;
UserModel get user => _userModel.value;
set user(UserModel value) => this._userModel.value = value;
void clear() {
_userModel.value = UserModel();
}
}
authController.dart
class AuthController extends GetxController {
FirebaseAuth _auth = FirebaseAuth.instance;
Rxn<FirebaseUser> _firebaseUser = Rxn<FirebaseUser>();
FirebaseUser get user => _firebaseUser.value;
@override
onInit() {
_firebaseUser.bindStream(_auth.onAuthStateChanged);
}
void createUserAccount(String userName, String email, String password) async {
try {
AuthResult _authResult = await _auth.createUserWithEmailAndPassword(
email: email.trim(), password: password);
//create user in database.dart
UserModel _user = UserModel(
userID: _authResult.user.uid, userName: userName, userEmail: email
);
if (await DatabaseServices().createNewUser(_user)) {
Get.find<UserController>().user = _user;
Get.back();
}
} catch (e) {
Get.snackbar(
"Error creating Account",
e.message,
);
}
}
void logInUser(String email, String password) async {
try {
AuthResult _authResult = await _auth.signInWithEmailAndPassword(
email: email.trim(), password: password);
Get.find<UserController>().user =
await DatabaseServices().getUser(_authResult.user.uid);
} catch (e) {
Get.snackbar(
"Error signing in",
e.message,
);
}
}
void logOutUser() async {
try {
await _auth.signOut();
Get.find<UserController>().clear();
} catch (e) {
Get.snackbar(
"Error signing out",
e.message,
);
}
}
}
authBinding.dart
class AuthBinding extends Bindings {
@override
void dependencies() {
Get.put<AuthController>(AuthController(), permanent: true);
}
}
user.dart(型号)
class UserModel {
String userID;
String userEmail;
String userName;
String userDisplayName;
String userProfilePhotoURL;
String userBioText;
int userScore;
UserModel(
{this.userID,
this.userEmail,
this.userName,
this.userDisplayName,
this.userProfilePhotoURL,
this.userBioText,
this.userScore});
UserModel.fromDocumentSnapshot(DocumentSnapshot doc) {
userID = doc.documentID;
userEmail = doc['userEmail'];
userName = doc['userName'];
userDisplayName = doc['userDisplayName'];
userProfilePhotoURL = doc['userProfilePhotoURL'];
userBioText = doc['userBioText'];
userScore = doc['userScore'];
}
}
您需要将类型插入 GetX
小部件。
return GetX<AuthController>( // add AuthController here
initState: (_) async {
Get.put<UserController>(UserController());
},
...
你的其他问题在这里
if (Get.find().user?.uid != null) {
return SMBottomNavBar();
}
如果你使用 Get.find
,同样的事情,总是提供类型。
Get.find<AuthController>().user?.uid
但由于您使用的是 GetWidget<AuthController>
,您可以这样做
if (controller.user?.uid != null) {
return SMBottomNavBar();
}
...
首先,我是第一次提问,如果我问的不正确和不完整,我会立即给你你想要的信息。
嘿,我正在按照本视频中的教程进行操作,但出现了这样的错误。 视频教程:https://www.youtube.com/watch?v=BiV0DcXgk58&t=314s&ab_channel=TadasPetra
错误是这样的: Error simulator picture
我收到此错误,但帐户是在 firebase Auth 中创建的,我想要的数据保存在 FireStore 数据库中。
颤振版本:2.2.3
pubspec.yaml
cloud_firestore: ^0.13.5
firebase_core: ^0.4.5
firebase_storage: ^3.1.6
get: ^4.3.8
错误调试控制台文本
[GETX] Instance "AuthController" has been created
[GETX] Instance "AuthController" has been initialized
[GETX] Instance "GetMaterialController" has been created
[GETX] Instance "GetMaterialController" has been initialized
════════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown building Root:
Null check operator used on a null value
The relevant error-causing widget was
Root
lib/main.dart:20
When the exception was thrown, this was the stack
#0 GetXState.initState
package:get/…/rx_flutter/rx_getx_widget.dart:78
#1 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4711
#2 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4548
#3 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3611
#4 Element.updateChild
package:flutter/…/widgets/framework.dart:3363
所有文件
Main.dart代码
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
initialBinding: AuthBinding(),
theme: ThemeData(scaffoldBackgroundColor: bgColor),
home: Root(),
);
}
}
Root.dart代码
class Root extends GetWidget<AuthController> {
@override
Widget build(BuildContext context) {
return GetX(
initState: (_) async {
Get.put<UserController>(UserController());
},
builder: (_) {
if (Get.find().user?.uid != null) {
return SMBottomNavBar();
} else {
return SignInScreen();
}
},
);
}
}
userController.dart
class UserController extends GetxController {
Rx<UserModel> _userModel = UserModel().obs;
UserModel get user => _userModel.value;
set user(UserModel value) => this._userModel.value = value;
void clear() {
_userModel.value = UserModel();
}
}
authController.dart
class AuthController extends GetxController {
FirebaseAuth _auth = FirebaseAuth.instance;
Rxn<FirebaseUser> _firebaseUser = Rxn<FirebaseUser>();
FirebaseUser get user => _firebaseUser.value;
@override
onInit() {
_firebaseUser.bindStream(_auth.onAuthStateChanged);
}
void createUserAccount(String userName, String email, String password) async {
try {
AuthResult _authResult = await _auth.createUserWithEmailAndPassword(
email: email.trim(), password: password);
//create user in database.dart
UserModel _user = UserModel(
userID: _authResult.user.uid, userName: userName, userEmail: email
);
if (await DatabaseServices().createNewUser(_user)) {
Get.find<UserController>().user = _user;
Get.back();
}
} catch (e) {
Get.snackbar(
"Error creating Account",
e.message,
);
}
}
void logInUser(String email, String password) async {
try {
AuthResult _authResult = await _auth.signInWithEmailAndPassword(
email: email.trim(), password: password);
Get.find<UserController>().user =
await DatabaseServices().getUser(_authResult.user.uid);
} catch (e) {
Get.snackbar(
"Error signing in",
e.message,
);
}
}
void logOutUser() async {
try {
await _auth.signOut();
Get.find<UserController>().clear();
} catch (e) {
Get.snackbar(
"Error signing out",
e.message,
);
}
}
}
authBinding.dart
class AuthBinding extends Bindings {
@override
void dependencies() {
Get.put<AuthController>(AuthController(), permanent: true);
}
}
user.dart(型号)
class UserModel {
String userID;
String userEmail;
String userName;
String userDisplayName;
String userProfilePhotoURL;
String userBioText;
int userScore;
UserModel(
{this.userID,
this.userEmail,
this.userName,
this.userDisplayName,
this.userProfilePhotoURL,
this.userBioText,
this.userScore});
UserModel.fromDocumentSnapshot(DocumentSnapshot doc) {
userID = doc.documentID;
userEmail = doc['userEmail'];
userName = doc['userName'];
userDisplayName = doc['userDisplayName'];
userProfilePhotoURL = doc['userProfilePhotoURL'];
userBioText = doc['userBioText'];
userScore = doc['userScore'];
}
}
您需要将类型插入 GetX
小部件。
return GetX<AuthController>( // add AuthController here
initState: (_) async {
Get.put<UserController>(UserController());
},
...
你的其他问题在这里
if (Get.find().user?.uid != null) {
return SMBottomNavBar();
}
如果你使用 Get.find
,同样的事情,总是提供类型。
Get.find<AuthController>().user?.uid
但由于您使用的是 GetWidget<AuthController>
,您可以这样做
if (controller.user?.uid != null) {
return SMBottomNavBar();
}
...