【问题标题】:Firebase/Flutter : Unhandled Exception: NoSuchMethodError: The getter 'isNotEmpty' was called on nullFirebase/Flutter:未处理的异常:NoSuchMethodError:getter 'isNotEmpty' 在 null 上被调用
【发布时间】:2021-07-15 04:12:06
【问题描述】:

所以我正在尝试使用我在表单字段中收集的变量在 firestore 中创建包含文档的集合,代码如下:

onPressed:() async {
              await FirebaseFirestore.instance
                  .collection(_ICO).add({
                  "Company name": _compname,
              }).then((_){
                print("success!");
              });

这是我得到的错误代码:

F/crash_dump32(18407): crash_dump.cpp:474] 未能附加到线程 181:权限被拒绝 I/e.firebase_com(17852): Thread[5,tid=17868,WaitingInMainSignalCatcherLoop,Thread=0xe1a8d210,peer=0x13240228,"Signal Catcher"]: 对信号 3 做出反应 我/e.firebase_com(17852): I/e.firebase_com(17852):将堆栈跟踪写入墓碑 E/flutter (17852): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:NoSuchMethodError:getter 'isNotEmpty' 在 null 上被调用。 E/颤振(17852):接收器:空 E/flutter (17852):尝试调用:isNotEmpty*

我还应该提到,我在尝试运行此代码时使用用户帐户登录。

感谢每一个帮助。

【问题讨论】:

标签: firebase flutter


【解决方案1】:

好吧,我终于找到了我的问题的答案,这真是愚蠢的错误,或者更确切地说是 2...

第一个:

Widget _buildICO(){
  return TextFormField(
    decoration: InputDecoration(labelText: 'ICO'),
    validator: (String value){
      if (value.isEmpty){
        return 'ICO je potrebné';
      }
    },
    onSaved: (String value){
      ICO = value;
    },
  );

}

你可能已经看到我有“onSaved”函数,按下按钮时我不会调用它,因此发送空值。

第二个:

尚未为此创建具有正确功能的适当变量:

Map<String , dynamic> data(){
    return{
   'compname': compname,
   'ICO': ICO,
    };
  }

我的按钮现在看起来像:

onPressed:() async {
                  if(!_formKey.currentState.validate()){
                    return;
                  }
                  _formKey.currentState.save();
                  firestore
                      .collection(ICO)
                      .doc(compname)
                      .set(data());

当我回到它并尝试将文档写入一个空集合并发现我的值返回为 null 后,我发现了问题所在(因为您无法创建空集合,但您可以将空值保存到文件),

希望我的问题对未来的人有所帮助。

【讨论】:

    【解决方案2】:

    您可以更改规则,使数据库只能由经过身份验证的用户读取/写入:

    {
      "rules": {
        ".read": "auth != null",
        ".write": "auth != null"
      }
    }
    

    { "rules": { ".read": true, ".write": true, } }
    

    要始终可用,记得在生产中更改它

    转到数据库,然后转到数据库规则,将规则更改为 true。

    【讨论】:

    • 由于这是我的测试项目,我的数据库可供所有人访问,而不仅仅是用户
    • { "rules": { ".read": true, ".write": true, } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 2020-12-09
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 2020-09-28
    • 2018-09-10
    相关资源
    最近更新 更多