2

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)],));
  }
}
DAni M
  • 683
  • 1
  • 10
  • 21
  • Have you tried passing the document ID in `doc()` like `.collection('users').doc('DOC_ID');` as in the [documentation](https://firebase.google.com/docs/firestore/query-data/get-data#dart_1)? – Dharmaraj Jun 26 '22 at 04:52
  • Hi , The random id so i need get them in detail page based on documentsnapshot and show the respective data in Text(). I added the main page to the code as well – DAni M Jun 26 '22 at 05:08
  • I can't see any navigator related code that redirects user to detail page but checkout the [documentation](https://docs.flutter.dev/cookbook/navigation/navigate-with-arguments) on passing arguments with navigator. You can read them in detail widget and then query Firestore. If the sample code in docs doesn't work or there's any issue, then please update the question with the same. – Dharmaraj Jun 26 '22 at 05:14
  • @Dharmaraj really appreciates the help. under the Inkwell. – DAni M Jun 26 '22 at 05:20
  • [This answer](https://stackoverflow.com/questions/53861302/passing-data-between-screens-in-flutter#:~:text=To%20send%20data%20to%20the%20next%20screen%20you%20do%20the%20following%20things) should help. You can pass the ID directly in `DetailPage(id: ID)` and adding an ID property in that class. – Dharmaraj Jun 26 '22 at 05:27

0 Answers0