Flutter/cloud_firestore: 运算符 '[]' 没有为类型 Map <String, dynamic> Function() 定义

Flutter/cloud_firestore: The operator '[]' isn't defined for the type Map <String, dynamic> Function()

我更新了我的 Firestore 包,但我收到了这个错误(运算符“[]”没有为类型 Map Function() ”)。我在另一个 post 中看到了同样的错误 ,但在我的例子中,我不确定如何进行更改,所以我用我的代码开始了一个新线程。任何人都可以告诉我如何进行更改吗?错误显示在下面的代码中 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,
                          ),

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

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

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