gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:38:15 26 4
gpt4 key购买 nike

我正在尝试从 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com