I am trying to design a feature using flutter that when a user selects a thumbnail image of a trip it will take the user to a page that has more details of the trip they selected. I am trying to query the Firestore db as little as possible so I was trying to get the document ID from a single query snapshot and pass it to the IndividualTripPackage class. I have tried this approach numerous ways but all of them have failed. I alsow looked at other solutions people posted on SO and I could not get them to work for my specific case. What am I doing wrong? I am new to flutter so if you have ideas about other approaches or more efficient solutions I am open to suggestions.
TripPackages Class:
class _TripPackagesState extends State<TripPackages> {
@override
Widget build(BuildContext context) {
//Some other code......
child: SingleChildScrollView(
child: StreamBuilder<QuerySnapshot>(
stream:
Firestore.instance.collection('trip_package').snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> docSnapshot) {
if (!docSnapshot.hasData) return const Text('Loading...');
final int docCount = docSnapshot.data.documents.length;
return GridView.builder(
shrinkWrap: true,
primary: false,
scrollDirection: Axis.vertical,
itemCount: docCount,
itemBuilder: (_, int index) {
DocumentSnapshot document =
docSnapshot.data.documents[index];
return GestureDetector(
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (_) => IndividualTripPackage(
docID: docSnapshot.data.documents[index]),
),
//Some other code .....
}
}
IndividualTripPackage Class:
class IndividualTripPackage extends StatefulWidget {
DocumentSnapshot docID;
IndividualTripPackage({this.docID});
@override
_IndividualTripPackageState createState() => _IndividualTripPackageState();
}
class _IndividualTripPackageState extends State<IndividualTripPackage> {
@override
Widget build(BuildContext context) {
final String docID = widget.docID.data['docID'];
return Material(
child: SafeArea(
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints viewportConstraints) {
return SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: viewportConstraints.maxHeight),
child: StreamBuilder(
stream: Firestore.instance.collection('trip_package').document('docID').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text('Loading data....Please wait...');
} else {
final int itemCount = snapshot.data.document('docID').data['itineraryItems'].length;
return Column(...);
}
}),
//Some more code........
}
}