Function Flatter/cloud_firestore:操作员';[]和#x27;isn';t为类型映射定义<;字符串,动态>;函数()
我更新了我的Firestore软件包,并收到此错误(“没有为类型映射函数()定义运算符“[]”)。我在另一篇文章中看到了同样的错误,但在我的例子中,我不确定如何进行更改,所以我用我的代码开始了一个新的线程。有人能告诉我怎么做这些改变吗?错误显示在下面的代码中,Function Flatter/cloud_firestore:操作员';[]和#x27;isn';t为类型映射定义<;字符串,动态>;函数(),function,flutter,dart,google-cloud-firestore,deprecated,Function,Flutter,Dart,Google Cloud Firestore,Deprecated,我更新了我的Firestore软件包,并收到此错误(“没有为类型映射函数()定义运算符“[]”)。我在另一篇文章中看到了同样的错误,但在我的例子中,我不确定如何进行更改,所以我用我的代码开始了一个新的线程。有人能告诉我怎么做这些改变吗?错误显示在下面的代码中,List categories=document.data['listCategories']??[]; TextFormField(
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,
),
TextFormField(
控制器:_catController,
模糊文本:false,
自动更正:错误,
一旦更改:(值){},
装饰:输入装饰(
labelText:“在此处添加新类别”,
标签样式:文本样式(
颜色:主题(上下文)。原色),
边框:大纲输入边框(
边界半径:边界半径。圆形(5.0),
),
聚焦顺序:大纲输入边框(
边界边(
宽度:2,
颜色:主题(上下文)。原色),
),
后缀:图标按钮(
图标:图标(
//根据passwordVisible状态选择图标
Icons.add,
颜色:主题。背景。原色,
),
onPressed:()异步{
最终字符串名称=_catController.text;
文档参考docRef=
userCollection.doc(uid);
DocumentSnapshot document=wait docRef.get();
列出类别=
document.data['listCategories']??[];//此错误运算符“[]”未为类型映射函数定义()
告诉您尝试对其执行[]
运算符的对象是一个函数。在您的情况下,.data
实际上不是一个成员变量,而是一个函数。只需添加()
在数据
关键字旁边,因此您的错误行(已修复)如下所示:
document.data()
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,
),
这可能是因为您更新了firebase core软件包。它以前只是.data[]
,但现在是.data()[]