'类型'_InternalLinkedHashMap <动态,动态>'不是类型'地图<字符串,动态>'的子类型'是我收到的错误

时间:2020-08-13 17:04:59

标签: firebase flutter google-cloud-firestore

尝试将表单数据上传到Cloud Firestore。我尝试过地图和哈希图的不同组合,但无法正常工作。同一程序能够无缝地从存储中读取数据,但不能写入。帮助将不胜感激。

这是代码:

MaterialButton(child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(Icons.add_circle),
                            Text('Add item'),
                          ]),
                      onPressed: () => {
                        setState(() {
                          if (_formKey.currentState.validate()) {
                            _formKey.currentState.save();
                            HashMap<dynamic, dynamic> listItem =
                                new HashMap<dynamic, dynamic>();
                            listItem["Description"] = _itemName;
                            listItem["Due"] = _due;
                            listItem["ID"] = _id;
                            listItem["Priority"] = _prioritySwitch;
                            listItem["Progress"] = _progress;
                            listItem["Reminder"] = _reminderSwitch;
                            listItem["SliderSwitch"] = _sliderSwitch;
                            listItem["Start"] = _start;

                            Firestore.instance
                                .collection("Task Lists")
                                .add(listItem.);
                          }
                          ;
                        }),
                      },
                    ),
                  ))
            ],
          ),
        ),

2 个答案:

答案 0 :(得分:0)

只需将HashMap的类型更改为Map:

MaterialButton(child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(Icons.add_circle),
                            Text('Add item'),
                          ]),
                      onPressed: () => {
                        setState(() {
                          if (_formKey.currentState.validate()) {
                            _formKey.currentState.save();
                            Map<String, dynamic> listItem =
                                new Map<String, dynamic>();
                            listItem["Description"] = _itemName;
                            listItem["Due"] = _due;
                            listItem["ID"] = _id;
                            listItem["Priority"] = _prioritySwitch;
                            listItem["Progress"] = _progress;
                            listItem["Reminder"] = _reminderSwitch;
                            listItem["SliderSwitch"] = _sliderSwitch;
                            listItem["Start"] = _start;

                            Firestore.instance
                                .collection("Task Lists")
                                .add(listItem.);
                          }
                          ;
                        }),
                      },
                    ),
                  ))
            ],
          ),
        ),

答案 1 :(得分:0)

尝试改用JSON格式,然后保存对Map的显式创建:

  Firestore.instance.collection("Task Lists").add(<String, dynamic>
  {
    "Description" : _itemName,
    "Due" : _due,
    "ID" : _id,
    "Priority" : _prioritySwitch,
    "Progress" : _progress,
    "Reminder" : _reminderSwitch,
    "SliderSwitch" : _sliderSwitch,
    "Start" : _start,
  });