如何解决可为空和不可为空的值
我使用 flutter 和 firebase 进行用户身份验证。
我遇到的问题是以下代码段:
import 'package:flutter/material.dart';
class AuthClass {
final FirebaseAuth auth = FirebaseAuth.instance;
//Signing in a user
Future<String> signIn({String email,String password}) async {
try {
await auth.signInWithEmailAndPassword(
email: email,password: password
);
return "Welcome!";
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
return 'No user found for that email.';
} else if (e.code == 'wrong-password') {
return 'Wrong password provided for that user.';
}
}
}
}
我调用 signIn 函数的上下文就在这里:
import 'package:flutter_client_app/Provider/auth_provider.dart';
import 'package:flutter_client_app/Screens/home.dart';
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
TextEditingController _email = TextEditingController();
TextEditingController _password = TextEditingController();
bool isLoading = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Login"),),body: isLoading == false ? Padding(
padding: const EdgeInsets.all(8.0),child: Column(
children: [
TextFormField(
controller: _email,decoration: InputDecoration(
hintText: "Email"
)
),const SizedBox(height: 30,TextFormField(
controller: _password,decoration: InputDecoration(
hintText: "Password"
)
),FlatButton( <=============================================================
color: Colors.blue,onPressed: () {
print (_email.text);
print (_password.text);
setState(() {
isLoading = true;
});
AuthClass()
.signIn(
email: _email.text.trim(),password: _password.text.trim())
.then((value) {
if (value == "Welcome!") {
setState(() {
isLoading = false;
});
Navigator.pushAndRemoveUntil(
context,MaterialPageRoute(builder: (context) => HomePage()),(route) => false);
} else {
setState(() {
isLoading = false;
});
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(value)));
}
}); <==============================================================
},child: Text("Login to account")),SizedBox(
height: 20,// GestureDetector(
// onTap: () {
// Navigator.pushAndRemoveUntil(
// context,// MaterialPageRoute(
// builder: (context) => RegisterPage()),(route) => false);
// },// child: Text("Don't have an account? Register"),// ),//
// const SizedBox(
// height: 10,MaterialPageRoute(builder: (context) => ResetPage()),// child: Text("Forgot Password? reset"),//),],) : Center(
child: CircularProgressIndicator(),)
);
}
}
我得到的错误如下:
29:33:错误:参数“email”的值不能为“null”,因为其类型为“String”,但隐式默认值为“null”。
29:47:错误:参数“password”的值不能为“null”,因为其类型为“String”,但隐式默认值为“null”。
29:18:错误:必须返回非空值,因为返回类型“字符串”不允许为空。
我已成功打印出 TextFormField 中记录的值,但在调用 signIn 异步函数时出现了问题。
解决方法
您正在使用 null-safety
功能。您可以通过将 pubspec.yaml
中的 dart sdk 更改为 environment: sdk: ">=2.7.0 <3.0.0"
而不是 environment: sdk: ">=2.12.0 <3.0.0"
来禁用它,或者将您的课程设置为 null-safety
。此外,您应该在 else
部分返回 if
of catch
的值。
import 'package:flutter/material.dart';
class AuthClass {
final FirebaseAuth auth = FirebaseAuth.instance;
//Signing in a user with nullable parameters
Future<String> signIn({String? email,String? password}) async {
try {
await auth.signInWithEmailAndPassword(
email: email as String,password: password as String,);
return "Welcome!";
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
return 'No user found for that email.';
} else if (e.code == 'wrong-password') {
return 'Wrong password provided for that user.';
}
else return "No Clear Error"
}
}
//Signing in a user with default value parameters
Future<String> signIn({String email = "",String password = ""}) async {
try {
await auth.signInWithEmailAndPassword(
email: email,password: password,);
return "Welcome!";
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
return 'No user found for that email.';
} else if (e.code == 'wrong-password') {
return 'Wrong password provided for that user.';
}
else return "No Clear Error"
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。