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