Firestoreからドキュメント一覧を取得ときに出てきたエラーの解消法を記述します。
FirebaseFirestore.instance.collection('users').getDocuments();
は非推奨で、
Listをsnapshotから取り出すときのdocuments
も非推奨のようです。
古いテキストで時々みるので、.getDocuments()をget()
にしましょう。
getDocuments()
のエラー
The method 'getDocuments' isn't defined for the type 'CollectionReference'.
Try correcting the name to the name of an existing method, or defining a method named 'getDocuments'.
documents
のエラー
The getter 'documents' isn't defined for the type 'QuerySnapshot<Map<String, dynamic>>'.
Try importing the library that defines 'documents', correcting the name to the name of an existing getter, or defining a getter or field named 'documents'.
非推奨の書き方
main.dart
class _MyFirestorePageState extends State<MyFirestorePage> {
List<DocumentSnapshot> documentList = [];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: <Widget>[
ElevatedButton(
child: Text('ドキュメント一覧取得'),
onPressed: () async {
final snapshot =
await FirebaseFirestore.instance.collection('users').getDocuments();
setState(() {
documentList = snapshot.documents;
});
},
),
],
),
),
);
}
}
新しい書き方
main.dart
class _MyFirestorePageState extends State<MyFirestorePage> {
List<DocumentSnapshot> documentList = [];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: <Widget>[
ElevatedButton(
child: Text('ドキュメント一覧取得'),
onPressed: () async {
final snapshot =
await FirebaseFirestore.instance.collection('users').get();
setState(() {
documentList = snapshot.doc;
});
},
),
],
),
),
);
}
}
このように書くとエラーがでなくなります。