[英]"Null check operator used on a Null Value" in Flutter
我對 Flutter 相當陌生,我正在使用 Firebase 開發一個聊天應用程序。 每當我運行應用程序時,我都會面臨這個“用於 Null 值的 Null Check 運算符”。 這是錯誤:
======== Exception caught by widgets library =======================================================
The following _CastError was thrown building SignIn(dirty, state: _SignInState#ccbd1):
Null check operator used on a null value
The relevant error-causing widget was:
SignIn file:///D:/Android/chat_app/lib/helper/authenticate.dart:24:14
When the exception was thrown, this was the stack:
#0 _SignInState.signIn (package:chat_app/views/signin.dart:32:29)
#1 _SignInState.build (package:chat_app/views/signin.dart:98:28)
#2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27)
#3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
#4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
...
====================================================================================================
這是SignIn.dart :
import 'package:chatapp/helper/helperfunctions.dart';
import 'package:chatapp/helper/theme.dart';
import 'package:chatapp/services/auth.dart';
import 'package:chatapp/services/database.dart';
import 'package:chatapp/views/chatrooms.dart';
import 'package:chatapp/views/forgot_password.dart';
import 'package:chatapp/widget/widget.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class SignIn extends StatefulWidget {
final Function toggleView;
SignIn(this.toggleView);
@override
_SignInState createState() => _SignInState();
}
class _SignInState extends State<SignIn> {
TextEditingController emailEditingController = new TextEditingController();
TextEditingController passwordEditingController = new TextEditingController();
AuthService authService = new AuthService();
final formKey = GlobalKey<FormState>();
bool isLoading = false;
signIn() async {
if (formKey.currentState.validate()) {
setState(() {
isLoading = true;
});
await authService
.signInWithEmailAndPassword(
emailEditingController.text, passwordEditingController.text)
.then((result) async {
if (result != null) {
QuerySnapshot userInfoSnapshot =
await DatabaseMethods().getUserInfo(emailEditingController.text);
HelperFunctions.saveUserLoggedInSharedPreference(true);
HelperFunctions.saveUserNameSharedPreference(
userInfoSnapshot.documents[0].data["userName"]);
HelperFunctions.saveUserEmailSharedPreference(
userInfoSnapshot.documents[0].data["userEmail"]);
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => ChatRoom()));
} else {
setState(() {
isLoading = false;
//show snackbar
});
}
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: appBarMain(context),
body: isLoading
? Container(
child: Center(child: CircularProgressIndicator()),
)
: Container(
padding: EdgeInsets.symmetric(horizontal: 24),
child: Column(
children: [
Spacer(),
Form(
key: formKey,
child: Column(
children: [
TextFormField(
validator: (val) {
return RegExp(
r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
.hasMatch(val)
? null
: "Please Enter Correct Email";
},
controller: emailEditingController,
style: simpleTextStyle(),
decoration: textFieldInputDecoration("email"),
),
TextFormField(
obscureText: true,
validator: (val) {
return val.length > 6
? null
: "Enter Password 6+ characters";
},
style: simpleTextStyle(),
controller: passwordEditingController,
decoration: textFieldInputDecoration("password"),
),
],
),
),
SizedBox(
height: 16,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ForgotPassword()));
},
child: Container(
padding: EdgeInsets.symmetric(
horizontal: 16, vertical: 8),
child: Text(
"Forgot Password?",
style: simpleTextStyle(),
)),
)
],
),
SizedBox(
height: 16,
),
GestureDetector(
onTap: () {
signIn();
},
child: Container(
padding: EdgeInsets.symmetric(vertical: 16),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
gradient: LinearGradient(
colors: [
const Color(0xff007EF4),
const Color(0xff2A75BC)
],
)),
width: MediaQuery.of(context).size.width,
child: Text(
"Sign In",
style: biggerTextStyle(),
textAlign: TextAlign.center,
),
),
),
SizedBox(
height: 16,
),
Container(
padding: EdgeInsets.symmetric(vertical: 16),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.white),
width: MediaQuery.of(context).size.width,
child: Text(
"Sign In with Google",
style:
TextStyle(fontSize: 17, color: CustomTheme.textColor),
textAlign: TextAlign.center,
),
),
SizedBox(
height: 16,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Don't have account? ",
style: simpleTextStyle(),
),
GestureDetector(
onTap: () {
widget.toggleView();
},
child: Text(
"Register now",
style: TextStyle(
color: Colors.white,
fontSize: 16,
decoration: TextDecoration.underline),
),
),
],
),
SizedBox(
height: 50,
)
],
),
),
);
}
}
Android Studio 將我重定向到 32:29 的這段代碼 ->
signIn() {
if (formKey.currentState!.validate()) {
HelperFunctions.saveUserEmailSharedPreference(emailTextEditingController.text);
setState(() {
isLoading = true;
});
databaseMethods.getUserByUserEmail(emailTextEditingController.text).then((val) {
snapShotUserInfo = val;
HelperFunctions
.saveUserNameSharedPreference(snapShotUserInfo?.docs[0].data()["name"]);
});
authMethods.signInWithEmailAndPassword(emailTextEditingController.text, passwordTextEditingController.text)
.then((val) {
if (val != null) {
HelperFunctions.saveUserLoggedInSharedPreference(true);
Navigator.pushReplacement(context,
MaterialPageRoute(
builder: (context) => ChatRoom())
);
}
});
}
}
一些幫助將不勝感激。
在登錄中定義使用
QuerySnapshot <dynamic>? snapshotUserInfo;
並用於空檢查
HelperFunctions
.saveUserNameSharedPreference(snapshotUserInfo!.docs[0].data()["name"]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.