我正在尝试从 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 https://pub.dev/packages/cloud_firestore/changelog
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)