我在 android studio 中使用顫振。編碼如下。
class _loginscreenState extends State<loginscreen> {
GlobalKey<FormState> formKey = GlobalKey<FormState>();
String? _email, _password;
void signin() async {
await FirebaseAuth.instance
.signInWithEmailAndPassword(email: _email, password: _password)
.catchError((onError) {
print(onError);
}).then((authUser) => {print(authUser.user?.uid)});
}
請幫我解決為什么代碼無法運行以及“'String”的含義是什么?可以為空,而“字串”不是”。解決方案是什么?
uj5u.com熱心網友回復:
void signin() async {
await FirebaseAuth.instance .signInWithEmailAndPassword(
email: _email! /* <-- */,
password: _password! /* <-- */).catchError((onError) {
print(onError);
}).then((authUser) => print(authUser.user?.uid));
}
嘗試添加 null(!)
uj5u.com熱心網友回復:
class _loginscreenState extends State<loginscreen> {
GlobalKey<FormState> formKey = GlobalKey<FormState>();
String? _email, _password;
void signin() async {
await FirebaseAuth.instance
//here change email -->email.tostring() and passsword--->password.tostring()
.signInWithEmailAndPassword(email: _email.toString(), password: _password.toString())
.catchError((onError) {
print(onError);
}).then((authUser) => {print(authUser.user?.uid)});
}
在這里您必須提供電子郵件和密碼。如果是字串?它可能包含也可能不包含空值 .so 避免email.tostring()我們可以使用tostring().if email 是空值,添加后它將轉換為“Null” email.toString()。
uj5u.com熱心網友回復:
這是由于在您的應用程式中使用了合理的 null 安全性,并且您將一個可為 null 的字串傳遞給一個不可為 null 的字串,一些解決方案是:
- 您可以斷言 _email、_password 的值是非空字串
- 或使用 bang 運算子
有關此字串的更多詳細資訊是 null 并且字串不是字串 或如何決議引數型別字串...
uj5u.com熱心網友回復:
您已將String? _email, _password;電子郵件和密碼設為可空。之后,您還沒有初始化電子郵件和密碼的值。FirebaseAuth.instance.signInWithEmailAndPassword并且您直接使用其中不支持空值的電子郵件和密碼。嘗試初始化電子郵件和密碼的值。
如果您從文本欄位中獲取值,則可以使用 TextEditingController。并將電子郵件和密碼的值作為引數傳遞。
TextEditingController emailController = TextEditingController();
TextEditingController passwordController = TextEditingController();
Future<String> signUp(
{required String email, required String password}) async {
final FirebaseAuth _auth;
await _auth
.createUserWithEmailAndPassword(email: email, password: password);
然后使用此功能。
signUp(email: nameController.text,password: passwordController.text);
uj5u.com熱心網友回復:
您可以使用空斷言運算子 (!),但請確保給定值不為空。
void signin() async {
await FirebaseAuth.instance .signInWithEmailAndPassword(
email: _email!, password: _password!,
) .catchError((onError) {
print(onError);
}).then((authUser) => {print(authUser.user?.uid)});
}
或者您可以在呼叫該方法之前檢查值是否不為空。
像那樣
void signin() async {
if(_email!=null&&_password!=null){
await FirebaseAuth.instance .signInWithEmailAndPassword(
email: **_email!**, password: **_password!**,
) .catchError((onError) {
print(onError);
}).then((authUser) => {print(authUser.user?.uid)});
}
}
uj5u.com熱心網友回復:
class _loginscreenState extends State<loginscreen> {
GlobalKey<FormState> formKey = GlobalKey<FormState>();
String _email, _password;
void signin() async {
await FirebaseAuth.instance
.signInWithEmailAndPassword(email: _email, password: _password)
.catchError((onError).toString() {
print(onError);
}).then((authUser) => {print(authUser.user?.uid)});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/435788.html
