I have this simple to query data from Firestore:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class TodoPage extends StatefulWidget {
const TodoPage({Key? key}) : super(key: key);
@override
State<TodoPage> createState() => _TodoPageState();
}
class _TodoPageState extends State<TodoPage> {
User? user = FirebaseAuth.instance.currentUser;
late final Stream<QuerySnapshot> _mainStream = FirebaseFirestore.instance
.collection('users')
.doc(user!.uid)
.collection('pendencies')
.snapshots();
@override
Widget build(BuildContext context) {
Size mediaQuery = MediaQuery.of(context).size;
return StreamBuilder<QuerySnapshot>(
stream: _mainStream,
builder: (context, mainSnapshot) {
if (mainSnapshot.hasError) {
return const Center(
child: Text('Something went wrong'),
);
}
if (mainSnapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
var pendenciesList = mainSnapshot.data!.docs;
print(pendenciesList);
return SafeArea(
child: SizedBox(
width: mediaQuery.width,
height: mediaQuery.height,
child: const Center(
child: Text('Test')
),
),
);
},
);
}
}
Currently, there are 2 documents in it. Is there a way to store the entire collection (documents and corresponded fields and values) inside a list? If yes, how can I do it?
I've tried var pendenciesList = mainSnapshot.data!.docs;
but got [Instance of '_JsonQueryDocumentSnapshot', Instance of '_JsonQueryDocumentSnapshot']
CodePudding user response:
The result of the snapshots()
function is of type Stream<QuerySnapshot<Map<String, dynamic>>>
meaning that it's like a stream of JSON values that you need to parse manually.
What you need to do is to define a function on your model object that can receive a value of type QueryDocumentSnapshot<Map<String, dynamic>> snapshot
and return a value of your model. Here is an example:
@immutable
class CloudNote {
final String documentId;
final String ownerUserId;
final String text;
const CloudNote({
required this.documentId,
required this.ownerUserId,
required this.text,
});
CloudNote.fromSnapshot(QueryDocumentSnapshot<Map<String, dynamic>> snapshot)
: documentId = snapshot.id,
ownerUserId = snapshot.data()[ownerUserIdFieldName],
text = snapshot.data()[textFieldName] as String;
}
Then when you retrieve your snapshots, you can convert them to your model object by mapping them as shown here:
Stream<Iterable<CloudNote>> allNotes({required String ownerUserId}) {
final allNotes = notes
.where(ownerUserIdFieldName, isEqualTo: ownerUserId)
.snapshots()
.map((event) => event.docs.map((doc) => CloudNote.fromSnapshot(doc)));
return allNotes;
}
CodePudding user response:
yes you can do that
//t he list where you have all the data
List data = [];
...
// function to fill up the data list
getData() async {
await FirebaseFirestore.instance.collection("collectionName").get.then((value) {
for(var i in value.docs) {
data.add(i.data());
}
});
}
...
// get a field from a document in the data list
data[index]["field"];