firebase - 类 'QueryDocumentSnapshot' 没有实例方法 '[]' 。接收器 : Instance of 'QueryDocumentSnapshot' Tried calling: [] ("name")

标签 firebase flutter google-cloud-firestore

我正在尝试从 FireStore 中检索这两个值

姓氏“test”(字符串)姓名“Carlos”(字符串)

但是我收到了这个错误

 *Another exception was thrown: NoSuchMethodError: Class 'QueryDocumentSnapshot' has no instance method '[]'.
 ═ Exception caught by widgets library ═
 The following NoSuchMethodError was thrown building StreamBuilder<QuerySnapshot> (dirty, state:
_StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#e1fff):
 
Class 'QueryDocumentSnapshot' has no instance method '[]'.
 
 Receiver: Instance of 'QueryDocumentSnapshot'
 
 Tried calling:[] ("name")

这是我的代码:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class HomePage extends StatelessWidget{

 List<Widget> makeListWiget(AsyncSnapshot snapshot){
   return snapshot.data.documents.map<Widget>((document){
     return ListTile(
       title: Text(document["name"]),
       subtitle: Text(document["lastname"]),

     );
   }).toList();

 }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
  appBar: AppBar(
    title: Text('Área do Cliente'),

  ),
    body: Container(
    child: StreamBuilder (
      stream: FirebaseFirestore.instance.collection("users").snapshots(),
      builder: (context, snapshot){
        return ListView(
          children: makeListWiget(snapshot),


        );
      }
   )

   )
   );

 }

}

最佳答案

来自cloud_firestore: ^0.14.0+2 版本,修改了很多方法和字段。

Need to use get() method for accessing particular field of a doucment like this document.get('user_name') instead of document['user_name']

So you have to access all fields as follows:
document.get('user_name')
document.get('user_mobile')
document.get('user_address')

还有其他方法,如 setData() 作为 set(),updateData() 作为 update()。

请检查文档 https://pub.dev/packages/cloud_firestore/changelog

关于firebase - 类 'QueryDocumentSnapshot' 没有实例方法 '[]' 。接收器 : Instance of 'QueryDocumentSnapshot' Tried calling: [] ("name"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63663853/

相关文章:

firebase - 是否有 Firestore 的 Dart 接口(interface) - 使用 https ://firebase. google.com/docs/firestore/quickstart 中的 API?

javascript - Firebase 云功能 - 连接错误

firebase - Firestore:合并2个流并同时发射两个值

Swift Firebase 必须是非空字符串且不包含 '.' '#' '$' '[' 或 ']'

flutter - 如何显示所有用户的倒计时时间

arrays - 为什么我无法检查 Flutter 列表是否为空?

javascript - 查询集合中所有游戏结果的平均分数?

android - Cloud Firestore 数据库结构

android - 恢复缓存数据 RecyclerView + Firestore

javascript - 网络应用 : Firestore cache - read changed documents only