【发布时间】:2021-10-24 01:01:37
【问题描述】:
我正在尝试从 firebase 数据库中获取数据。我收到此错误:
没有为类“对象”定义运算符“[]”
尝试了很多方法,但错误仍然存在。
StreamBuilder<QuerySnapshot>(
stream: _categoryProvider.category.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
if(snapshot.hasError){
return Text('Something went wrong..');
}
if(snapshot.connectionState == ConnectionState.waiting){
return Center(child: CircularProgressIndicator(),);
}
return Expanded(
child: ListView(
children: snapshot.data.docs.map((DocumentSnapshot document){
return ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(document?.data()['image']),// error is on this line
),
//title: Text(document.data()['category name']),
onTap: (){},
);
}).toList(),
),
);
}),
这里是 CategoryProvider。
import 'package:cloud_firestore/cloud_firestore.dart';
class CategoryProvider{
CollectionReference category = FirebaseFirestore.instance.collection('categories');
}
【问题讨论】:
-
我认为右括号在错误的地方
NetworkImage(document?.data())['image'] -
非常感谢您的时间和回答。现在这是错误
lib/widgets/category_list.dart:56:74: Error: The operator '[]' isn't defined for the class 'NetworkImage'. - 'NetworkImage' is from 'package:flutter/src/painting/image_provider.dart' ('/C:/flutter/packages/flutter/lib/src/painting/image_provider.dart'). Try correcting the operator to an existing operator, or defining a '[]' operator. backgroundImage: NetworkImage(document?.data())['image'],
标签: firebase flutter dart google-cloud-firestore flutter-streambuilder