user15585062
user15585062

Reputation:

How to get document id from firestore

I'm trying to get my every document id in of my collection "meinprofilsettings". But I'm a bit struggling with that so maybe anyone can help.

First here's my code:

List<String> alluserids = [];

getHashtags() async {
    final ref = FirebaseFirestore.instance;

    QuerySnapshot snapshots = await ref.collection('meinprofilsettings').get();

    for (QueryDocumentSnapshot idofuser in snapshots.docs) {
      allVideoHastags.addAll(idofuser.id);
    }
}

And then here's the error I get:

The argument type 'String' can't be assigned to the parameter type 'Iterable'.

This is a screenshot of my database:

enter image description here

I I just want every id of of every doc inside the list alluserids.

Upvotes: 0

Views: 739

Answers (2)

Huthaifa Muayyad
Huthaifa Muayyad

Reputation: 12343

Perhaps you want to change this

allVideoHastags.addAll(idofuser.id);

To this:

alluserids.add(idofuser.id);

Upvotes: 1

Luis Utrera
Luis Utrera

Reputation: 1067

addAll method receives an Iterable as parameter. You want to add a single value so you should use .add

Also, you could use the .map method instead of for in:

allVideoHastags.addAll(snapshots.docs.map((idofuser)=>idofuser.id));

The behavior behind the last statement is that map returns an Iterable which has a value for each value that the base Iterable (in this case, your docs) contains.

In this case, the base iterable is snapshots.docs and our mapping returns Iterable<String> as we're returning every doc id. Hope this is enough for helping

Upvotes: 0

Related Questions