【问题标题】:Unhandled Exception: Bad state: field does not exist within the DocumentSnapshotPlatform (While it does exits on firestore)未处理的异常:错误状态:DocumentSnapshotPlatform 中不存在字段(虽然它确实在 firestore 上退出)
【发布时间】: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


    【解决方案1】:

    问题解决了。他们是两件事。

    1. 我犯了一个错误,没有创建必填字段'name'。此外,您还必须检查,如果您在 Firestore 中使用小写字母而不是使用小写字母,并且如果您使用大写字母,那么在您的代码字段中也使用大写字母。 Firestore 对字母大小写敏感

    2. 在新版本的颤振中,命令已经改变,现在它也需要一个 data()

    这是对我有用的更新代码

    if(result['id'] != _adminController.text.trim())

    改为

    if(result.data()['id'] != _adminController.text.trim())

     loginAdmin () {
             FirebaseFirestore.instance.collection('admins').get().then((snapshot){
              snapshot.docs.forEach((result){
                if(result.data()['id'] != _adminController.text.trim())
                  {
                    Scaffold.of(context).showSnackBar(SnackBar(content: Text('ID is not Correct')));
                  }
                else if(result.data()['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.data()['name'],)));
                    setState(() {
                      _adminController.text='';
                      _passwordController.text='';
                    });
                    Route route=MaterialPageRoute(builder: (c)=> UploadPage());
                    Navigator.pushReplacement(context, route);
                }
              });
            });
    

    【讨论】:

      【解决方案2】:

      检查 Firestore 中的所有字段在您的代码中是否相同。 可能是这个字段(管理员)更改为管理员

      【讨论】:

      • firestore 和 code 中的字段相同检查多次。你也可以看到它。因为大多数时候,当我搜索错误时,人们会说检查你的字段,所以这就是为什么我为你上传了我的代码以及我的 firestore 字段,并且在模拟器中,当我按下登录按钮时,应用程序也会崩溃。
      • 另外请检查您更新的问题,我已经添加了另一张文件图片,它可能会帮助您了解我在做什么。
      • 将此字段 result['id'] 更改为 result['ID'] ,并将此字段添加到 firestore 'name'
      猜你喜欢
      • 2021-03-20
      • 2021-03-05
      • 2023-01-11
      • 1970-01-01
      • 2021-05-16
      • 2021-05-16
      • 2021-10-27
      • 2021-09-28
      • 2021-09-27
      相关资源
      最近更新 更多