我有一個管理員登錄頁面,管理員可以在其中添加 ID 和密碼以訪問管理區域。Firebase 資料庫中有一個管理集合,用于存盤 ID 和密碼。管理集合與用戶登錄頁面分開,并與在允許訪問之前使用 Firebase 身份驗證的用戶集合分開。用戶訪問繼續正常作業。當我填寫管理員登錄螢屏上的兩個輸入框并單擊按鈕訪問時,出現錯誤對話框訊息,指示即使有資料,兩個輸入欄位中也沒有任何資料。如果我對代碼什么都不做,然后熱多載并再次單擊該按鈕,我就可以訪問管理員,但我在控制臺中收到以下錯誤訊息。
在構建 ShoppingAdminSignInPage(dirty, dependencies: [_LocalizationsScope-[GlobalKey#2c797]], state: _ShoppingAdminSignInPageState#e3b3d): type 'Null' is not a subtype of type '() => void' 時拋出了以下 _TypeError
我顯然在我的代碼中寫了一些錯誤的東西。錯誤似乎出現在 ShoppingAdminSignInButton 中。預先感謝您的任何幫助。
class ShoppingAdminSignInPage extends StatefulWidget {
const ShoppingAdminSignInPage({Key? key}) : super(key: key);
@override
State<ShoppingAdminSignInPage> createState() =>
_ShoppingAdminSignInPageState();
}
class _ShoppingAdminSignInPageState extends State<ShoppingAdminSignInPage> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final TextEditingController _adminIDController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return AdaptiveLayoutScaffold(
appBar: const ShoppingAdminSignInPageAppBar(),
landscapeBodyWidget: Container(),
portraitBodyWidget: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
const ShoppingAdminSignInHeader(),
Form(
key: _formKey,
child: Column(
children: [
const SizedBox(
height: 50.0,
),
AdminSignInTextField(
controller: _adminIDController,
labelText: TextFieldLabel.adminID,
prefixIcon: Icons.person,
textInputAction: TextInputAction.next,
),
AdminSignInTextField(
controller: _passwordController,
labelText: TextFieldLabel.password,
prefixIcon: Icons.password,
textInputAction: TextInputAction.done,
),
ShoppingAdminSignInButton(
onPressed: _adminIDController.text.isNotEmpty &&
_passwordController.text.isNotEmpty
? logInAdmin()
: () => showDialog(
context: context,
builder: (ctx) {
return const ErrorAlertDialog(
message: DialogString.addAdminIDAndPassword,
);
}),
),
const NotAnAdminButton(),
],
),
),
],
),
),
),
);
}
logInAdmin() {
FirebaseFirestore.instance.collection('admins').get().then((snapshot) {
snapshot.docs.forEach((result) {
if (result.data()['id'] != _adminIDController.text.trim()) {
SnackBarUtil.showSnackBar(
context,
SnackBarString.idNotCorrect,
);
} else if (result.data()['password'] !=
_passwordController.text.trim()) {
SnackBarUtil.showSnackBar(
context,
SnackBarString.passwordNotCorrect,
);
} else {
SnackBarUtil.showSnackBar(
context,
'Welcome ${result.data()['name']}',
);
setState(() {
_adminIDController.text = '';
_passwordController.text = '';
});
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const UploadItemsPage(),
),
);
}
});
});
}
}
uj5u.com熱心網友回復:
onPressed: _adminIDController.text.isNotEmpty && _passwordController.text.isNotEmpty
? logInAdmin()
: () => showDialog(
上面,你是什么意思是,如果條件為真(在這種情況下,條件是既_adminIDController和_passwordController不為空),那么它應該運行logInAdmin并等待它完成,然后運行任何logInAdmin回傳。
Dart 認為logInAdmin將回傳一個函式,它應該運行該函式。事實并非如此,您希望按鈕直接運行logInAdmin。
要解決此問題,請洗掉括號:
onPressed: _adminIDController.text.isNotEmpty && _passwordController.text.isNotEmpty
? logInAdmin
: () => showDialog(
這樣,您不是在分配函式的結果,而是在分配函式本身。
同樣作為一般建議,您應該始終在函式上宣告回傳型別,以便 dart 可以在發生這種情況時告訴您
void logInAdmin() {
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/385222.html
標籤:火力基地 扑 镖 谷歌云firestore 颤振布局
上一篇:setDoc函式回傳什么?
下一篇:將物件添加到陣列而不覆寫
