【发布时间】:2021-10-26 00:30:14
【问题描述】:
我正在编写一个我试图登录管理员的代码。为此,我在 FireStore 上创建了一个管理员,不是使用代码,而是使用 firestore 提供的选项。就像我首先在 firestore 上创建了一个管理员,然后我编写了一个代码,在其中我试图通过在应用程序中提供相同的管理员 ID 和密码来登录该特定管理员。我想这是某种关键问题,但我不知道如何处理这个错误。
以下是应用在模拟器上使用时出现的错误
以下是应用在Real Android Mobile上使用时出现的错误
当点击第 7 行时,它会将我带到为管理员登录编写 Firestore 代码的文件。
更新:
当我单击下面的行错误(在实际手机上运行的应用程序)时,它会将我带到我不理解的 firestore 快照文件。
FirebaseFirestore 控制台(此处的管理字段是使用“+”选项创建的)
颤振医生
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.2.3, on Microsoft Windows [Version 10.0.19041.450], locale en-PK)
[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[√] Chrome - develop for the web
[√] Android Studio
[√] Connected device (2 available)
• No issues found!
AdminLogin.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:ecom_app/Authentication/AuthnticationScreen.dart';
import 'package:ecom_app/DialogBox/errordialog.dart';
import 'package:ecom_app/Widgets/CustomTextField.dart';
import 'package:flutter/material.dart';
import 'Uploaditems.dart';
class Adminsignin extends StatelessWidget {
const Adminsignin({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.pink,Colors.lightGreenAccent],
begin: const FractionalOffset(0.0, 0.0),
end: const FractionalOffset(1.0, 0.0),
stops: [0.0,1.0],
tileMode: TileMode.clamp,
)
),
),
title: Text
(
'E-Shop',
style: TextStyle(
fontSize:55.0,color: Colors.white),
),
centerTitle: true,
),
body: AdminSignInPage(),
);
}
}
class AdminSignInPage extends StatefulWidget {
const AdminSignInPage({Key? key}) : super(key: key);
@override
_AdminSignInPageState createState() => _AdminSignInPageState();
}
class _AdminSignInPageState extends State<AdminSignInPage> {
final TextEditingController _adminController =TextEditingController();
final TextEditingController _passwordController =TextEditingController();
final GlobalKey<FormState> _formKey=GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
double _screenwidth=MediaQuery.of(context).size.width,_screenHeight=MediaQuery.of(context).size.height;
return SingleChildScrollView(
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.pink,Colors.lightGreenAccent],
begin: const FractionalOffset(0.0, 0.0),
end: const FractionalOffset(1.0, 0.0),
stops: [0.0,1.0],
tileMode: TileMode.clamp,
)
),
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Container(
alignment: Alignment.bottomCenter,
child: Image.asset(
'images/welcome.png',
height: 240.0,
width: 240.0,
),
),
Padding(
padding:EdgeInsets.all(8.0),
child: Text('Admin',
style: TextStyle(color: Colors.white38,fontSize: 28.0,fontWeight: FontWeight.bold),
),
),
Form(
key: _formKey,
child: Column(
children: [
customtextfield(
controller: _adminController,
data: Icons.person,
hinttext: 'ID',
isObsecure:false,
),
customtextfield(
controller: _passwordController,
data: Icons.lock,
hinttext: 'Password',
isObsecure:true,
),
SizedBox(
height: 25.0,
),
ElevatedButton(
onPressed: ()=>
_adminController.text.isNotEmpty&&_passwordController.text.isNotEmpty
? loginAdmin ()
:showDialog(
context: context,
builder: (c){
return ErrorDialog(message: 'Please Enter ID and Password');
}
),
style: ElevatedButton.styleFrom(primary: Colors.pink,onPrimary: Colors.yellow),
child:Text("Login"),
),
SizedBox(
height: 50.0,
),
Container(
height: 4.0,
width: _screenwidth*0.8,
color: Colors.pink,
),
SizedBox(
height: 10.0,
),
FlatButton.icon(
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context)=>AuthenticScreen( )));
},
icon: Icon(Icons.admin_panel_settings,color: Colors.pink,),
label: Text('I am not an Admin',style: TextStyle(color: Colors.pink,fontWeight: FontWeight.bold),),
),
SizedBox(
height: 50.0,
),
],
)
),
],
),
),
);
}
loginAdmin (){
FirebaseFirestore.instance.collection('admins').get().then((snapshot){
snapshot.docs.forEach((result){
if(result['id'] != _adminController.text.trim())
{
Scaffold.of(context).showSnackBar(SnackBar(content: Text('ID is not Correct')));
}
else if(result['password' != _passwordController.text.trim()])
{
Scaffold.of(context).showSnackBar(SnackBar(content: Text('Passwod is not Correct')));
}
else
{
Scaffold.of(context).showSnackBar(SnackBar(content: Text('Welcome Admin, ' +result['name'],)));
setState(() {
_adminController.text='';
_passwordController.text='';
});
Route route=MaterialPageRoute(builder: (c)=> UploadPage());
Navigator.pushReplacement(context, route);
}
});
});
}
}
【问题讨论】:
标签: flutter google-cloud-firestore