类“QueryDocumentSnapshot”没有实例方法“[]”。接收器:'QueryDocumentSnapshot' 实例尝试调用:[] ("name")

Kep*_*uff 2 firebase flutter google-cloud-firestore

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

姓氏“测试”(字符串)名称“卡洛斯”(字符串)

但是我收到这个错误

 *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")
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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),


        );
      }
   )

   )
   );

 }

}
Run Code Online (Sandbox Code Playgroud)

Mah*_*i19 5

来自 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')
Run Code Online (Sandbox Code Playgroud)

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

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