Reputation: 75
I'm trying to retrieve those both values from FireStore
lastname "test" (string) name "Carlos" (String)
However I'm getting this error
*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")
Here's my code:
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),
);
}
)
)
);
}
}
Upvotes: 2
Views: 6419
Reputation: 402
From cloud_firestore: ^0.14.0+2 version, so many methods and fields have been changed.
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')
And also other methods like setData() as set(), updateData() as update().
Please check documention https://pub.dev/packages/cloud_firestore/changelog
Upvotes: 6
Reputation: 586
The object that you are trying to access with document["name"]
is not valid as the object is of instance QueryDocumentSnapshot and it doesnt have a method []. However it supports a get method, for further read on this link. I have made few changes to your code and below is a working version.
if not already added, please add firebase_core: ^0.5.0
as dependency in pubspec.yaml
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: Firebase.initializeApp(),
builder: (context, snapshot) {
final FirebaseFirestore _fireStore = FirebaseFirestore.instance;
return Scaffold(
appBar: AppBar(
title: Text('Área do Cliente'),
),
body: Container(
child: StreamBuilder(
stream: _fireStore
.collection("users")
.snapshots(),
builder: (context, snapshot) {
return ListView(
children: makeListWiget(snapshot),
);
})));
},
);
}
List<Widget> makeListWiget(AsyncSnapshot snapshot) {
return snapshot.data.documents.map<Widget>((document) {
return ListTile(
title: Text(document.get('name')),
subtitle: Text(document.get('lastname')),
);
}).toList();
}
}
Upvotes: 4