I have two collection in firestore question
and bookmark
.
for saving a question to users bookmarks page , my approach for structuring database was to set the documents docID
of bookmark collection to be same as of question collections documents docID
and field of bookmark collection is only one that is uid of user who is saving the bookmark my structure looks like this,
this is my bookmark collection
this is my question collection
here bookmark documents ID == question document ID
so far I have used streambuilder but not able to fetch the question documents from bookmark collection, this is my code so far and I aint getting how to get question documents from bookmarks collection docID
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:devcom/screens/detailsPage.dart';
import 'package:devcom/utils/responsive.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
class Bookmark extends StatefulWidget {
final User user;
const Bookmark({Key? key, required this.user}) : super(key: key);
@override
State<Bookmark> createState() => _BookmarkState();
}
class _BookmarkState extends State<Bookmark> {
@override
void initState() {
_currentUser = widget.user;
super.initState();
}
late User _currentUser;
// final db = FirebaseFirestore.instance;
@override
Widget build(BuildContext context) {
final Stream<QuerySnapshot> _bmStreams = FirebaseFirestore.instance
.collection('bookmark')
.where("uid", isEqualTo: "${_currentUser.uid}")
.snapshots(includeMetadataChanges: true);
return StreamBuilder<QuerySnapshot>(
stream: _bmStreams,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.hasData == false)
return Text(
'Howdy ${_currentUser.displayName}!!, 404 clever: you havent posted in a while');
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return Scaffold(
appBar: AppBar(
title: Text('My Questions'),
leading: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: Icon(Icons.arrow_back_ios_new_rounded)),
),
backgroundColor: Colors.white,
body: Padding(
padding: EdgeInsets.all(40),
child: ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, int index) {
final DocumentSnapshot data = snapshot.data!.docs[index];
if (!data.exists) {
print('not exists');
}
return Text(data['mydatahere']);
},
)));
},
);
}
}
and is it possible to nest streambuilder or do I need to fetch documents separtely please help I am badly stuck at this and dont know how to make this work...
thank you,
streamBuilder
, but consider using afuture
for thebookmark
collection. – Gisele