0
votes

I was wondering how can I display more than one document that is related to the same USER UID

enter image description here

For now My APP is only Displaying one pdf document

enter image description here

what can i do to display more than one document of the same user??? Im confuse, how this logic should work, is it possible to do this without SQL?

I only know how to get a single document using the user UID.

my code:

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
    FirebaseFirestore firestore = FirebaseFirestore.instance;
    FirebaseAuth auth = FirebaseAuth.instance;

    int _currentIndex = 0;

    @override
    Widget build(BuildContext context) {
      final User user = auth.currentUser;
      final uid = user.uid;



      final tabs = [
        Center(

            child: (Scaffold(
                body: FutureBuilder(
                    future: FirebaseFirestore.instance
                        .collection('users')
                        .doc(uid)
                        .get(),
                    builder: (context, AsyncSnapshot snapshot) {
                      if (snapshot.data == null)
                        return CircularProgressIndicator();

                      DocumentSnapshot manuais = snapshot.data;

                      if (snapshot.hasData) {
                        print('ok');
                        print(manuais.data()['nome']);
                      } else {
                        print('nopeeeee');
                      }
                      return Container(
                        padding: EdgeInsets.all(16),
                        child: ListView.builder(
                            itemCount : 1,
                            itemBuilder: (context, index) {



                              DocumentSnapshot manuais = snapshot.data;


                              return Card(
                                color: Colors.grey[250],
                                child: Container(
                                  padding: EdgeInsets.all(10),
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: <Widget>[
                                      new Image.asset(
                                        'Images/pdflogo.png',
                                        width: 32,
                                      ),
                                      Center(
                                        child: Text(
                                          (manuais.data()['nome'].toString()),
                                          maxLines: 1,
                                          overflow: TextOverflow.ellipsis,
                                          style: TextStyle(fontSize: 16),
                                        ),
                                      ),
                                      ButtonBar(
                                        children: <Widget>[
                                          FlatButton(
                                              child: const Text(
                                                  'Compartilhar / Download'),
                                              onPressed: () async {
                                                var request = await HttpClient()
                                                    .getUrl(Uri.parse(manuais
                                                        .data()['documento']));
                                                var response =
                                                    await request.close();
                                                Uint8List bytes =
                                                    await consolidateHttpClientResponseBytes(
                                                        response);
                                                await Share.file(
                                                    'ESYS AMLOG',
                                                    'Manual.pdf',
                                                    bytes,
                                                    'image/jpg');
                                              }),
                                        ],
                                      ),
                                    ],
                                  ),
                                ),
                              );
                            }),
                      );
                    })))),
1

1 Answers

2
votes

You cannot have several documents with the same ID in one Collection: a document ID must be unique across a collection.

In your case, if I correctly understand it, you could have a subcollection for each user document, like

-- users (collection)
   -- DTB2.... (user document)
      -- pdfDocs (subcollection)
         -- JH8766ng.... (pdf document, with auto-generated ID)
            fields: nome: "PDF1", creationDate: ..., author: .... (for example)
         -- 65rt6b8H.... (pdf document, with auto-generated ID)
            fields: nome: "PDF2", creationDate: ..., author: .... 

You would query all the docs with:

 FirebaseFirestore.instance
       .collection('users')
       .doc(uid)
       .collection('pdfDocs')
       .get();

which returns a QuerySnapshot.