为什么Firestore数据赋予类“ QueryDocumentSnapshot”没有实例方法“ []”。错误?

时间:2020-08-23 14:12:38

标签: firebase flutter dart

我正在尝试从Firestore获取图片,并且出现此错误, 数据库中图像的键是“图像1网址”

相同的代码和相同的结构可在我的其他应用程序上使用,但此应用程序会引发此错误

Class 'QueryDocumentSnapshot' has no instance method '[]'.
Receiver: Instance of 'QueryDocumentSnapshot'
Tried calling: []("image 1 Url")

这是firestore数据库结构, enter image description here

class Toyota extends StatefulWidget {
  @override
  _ToyotaState createState() => _ToyotaState();
}

class _ToyotaState extends State<Toyota> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: StreamBuilder(
          stream: FirebaseFirestore.instance.collection('toyota').snapshots(),
          builder: (context, snapshot) {
            if (snapshot.data == null)
              return Center(
                child: CircularProgressIndicator(
                  backgroundColor: Colors.red,
                  valueColor: new AlwaysStoppedAnimation<Color>(Colors.teal),
                ),
              );
            return ListView.builder(
                itemCount: snapshot.data.documents.length,
                itemBuilder: (context, index) => SingleChildScrollView(
                      child: Padding(
                        padding: const EdgeInsets.all(15.0),
                        child: Container(
                              decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(3)),
                          child: Image.network(
                            snapshot.data.documents[index]['image 1 Url'],
                            ),
                        ),
                      ),
                    ));
          },
        ));
  }
}

1 个答案:

答案 0 :(得分:4)

我发现使用get('String')的解决方案可以与我一起工作

select * from table where regexp_like(eventname, 'Execute|Error|Running' )

您可以查看更多内容:https://developers.google.com/android/reference/com/google/firebase/firestore/QueryDocumentSnapshot