【问题标题】:Flutter/cloud_firestore: The operator '[]' isn't defined for the type Map <String, dynamic> Function()Flutter/cloud_firestore:未为 Map <String, dynamic> Function() 类型定义运算符“[]”
【发布时间】:2021-03-23 21:41:26
【问题描述】:

我更新了我的 Firestore 包并收到此错误(“未为类型 Map Function() 定义运算符'[]'”)。我在另一篇帖子中看到了关于相同错误here 的内容,但就我而言,我不确定如何进行更改,因此我正在使用我的代码开始一个新线程。谁能告诉我如何进行更改,好吗?错误显示在下面的代码中 List categories = document.data['listCategories'] ?? [];

                          TextFormField(
                            controller: _catController,
                            obscureText: false,
                            autocorrect: false,
                            onChanged: (value) {},
                            decoration: InputDecoration(
                              labelText: 'Add a new category here',
                              labelStyle: TextStyle(
                                  color: Theme.of(context).primaryColor),
                              border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(5.0),
                              ),
                              focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(
                                    width: 2,
                                    color: Theme.of(context).primaryColor),
                              ),
                              suffixIcon: IconButton(
                                icon: Icon(
                                  // Based on passwordVisible state choose the icon
                                  Icons.add,
                                  color: Theme.of(context).primaryColor,
                                ),
                                onPressed: () async {
                                  final String name = _catController.text;
                                  DocumentReference docRef =
                                      userCollection.doc(_uid);
                                  DocumentSnapshot document = await docRef.get();
                                  List categories =
                                      document.data['listCategories'] ?? []; // <-- ERROR 
                                  if (categories.contains(name) == true) {
                                    setState(() {
                                      _errorMessage =
                                          '$name has already been added';
                                    });
                                  } else {
                                    userCollection.doc(_uid).update({
                                      'listCategories':
                                          FieldValue.arrayUnion([name])
                                    });
                                    _catController.text = '';
                                    setState(() {
                                      _errorMessage = '';
                                    });
                                  }
                                },
                              ),
                            ),
                            autovalidateMode: AutovalidateMode.always,
                            cursorColor: Theme.of(context).primaryColor,
                            maxLines: 1,
                          ),

【问题讨论】:

    标签: function flutter dart google-cloud-firestore deprecated


    【解决方案1】:

    此错误The operator '[]' isn't defined for the type Map &lt;String, dynamic&gt; Function() 告诉您,您尝试对其执行[] 运算符的对象是一个函数。在您的情况下,.data 实际上不是成员变量,而是一个函数。只需在 data 关键字旁边添加 () 即可解决此问题,因此您的错误行(已修复)如下所示:

    document.data()['listCategories'] ?? [];

    这可能是因为您更新了 firebase 核心包。以前只是.data[],但现在是.data()[]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 2020-12-27
      • 2020-12-15
      • 2022-01-05
      • 1970-01-01
      相关资源
      最近更新 更多