I retrieved all the documents in a Listviewbuilder in the main page but then the user click on one of the lists I want to go to a detail page and show the respective data,
This is the main page where we get all the documents. where we get them as a stream.
class MainPage extends StatefulWidget {
const MainPage({Key? key}) : super(key: key);
@override
State<MainPage> createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
final CollectionReference _firestoreRef =
FirebaseFirestore.instance.collection('users');
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color.fromARGB(255, 5, 24, 11),
appBar: AppBar(
title: Text('BlogPosts'),
backgroundColor: Color.fromARGB(255, 5, 41, 24),
),
body: StreamBuilder(
stream: _firestoreRef.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
if (streamSnapshot.hasError) {
return const Text('has error somewhere');
}
return ListView.builder(
shrinkWrap: true,
itemCount: streamSnapshot.data!.docs.length,
itemBuilder: ((context, index) {
final DocumentSnapshot documentSnapshot =
streamSnapshot.data!.docs[index];
return Container(
height: 180,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => DetailPage()),
);
},
child: Stack(
children: [
ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.network(
documentSnapshot['imgUrl'],
fit: BoxFit.cover,
width: MediaQuery.of(context).size.width,
),
),
Container(
height: 180,
color: Colors.black45.withOpacity(.3),
),
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
documentSnapshot['title'],
style: const TextStyle(
color: Colors.white,
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
SizedBox(
height: 3,
),
Text(
documentSnapshot['name'],
style: TextStyle(
color: Colors.white, fontSize: 18),
),
SizedBox(
height: 8,
),
Text(
documentSnapshot['blog'],
style: TextStyle(
color: Colors.white, fontSize: 16),
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
),
],
),
),
],
),
),
),
);
}),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const AddBlog()),
);
},
child: Icon(Icons.add),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
);
}
}
I want to go to a detail page and show the respective data,
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class DetailPage extends StatelessWidget {
DetailPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final docRef = FirebaseFirestore.instance.collection('users').doc();
docRef.get().then((DocumentSnapshot doc) {
final data = doc.data() as Map<String, dynamic>;
final String name = data['name'];
final String title = data['title'];
final String blog = data['blog'];
});
return Scaffold(
appBar: AppBar(
backgroundColor: Color.fromARGB(255, 77, 70, 70),
elevation: 0,
title: Text('blog'),
centerTitle: true,
),
body: Column(children: [Text(name)],));
}
}