Firebase/Flutter:未处理的异常:NoSuchMethodError:getter 'isNotEmpty' 在 null 上被调用

时间:2021-04-21 00:22:33

标签: firebase flutter

所以我试图用 firestore 中的文档创建集合,从我在 Form fiels 中收集的变量,代码如下:

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 做出反应 I/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/flutter (17852):接收器:空 E/flutter (17852):尝试调用:isNotEmpty*

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

感谢每一个帮助。

2 个答案:

答案 0 :(得分:1)

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

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

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

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

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

答案 1 :(得分:0)

好吧,我终于找到了我的问题的答案,这真的是愚蠢的错误,或者更确切地说是 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());

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

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