skip to Main Content

I need a list of document ID’s from a collection, so I can use it in a DropdownButton, which will select the specific document.

The code below prints out the doc.id for each document in the collection.

void getDevices () async {
  var firebaseUser = FirebaseAuth.instance.currentUser?.email;
  print('firebaseUser: $firebaseUser');
  await firestoreInstance.collection(devices).where('email', isEqualTo: '$firebaseUser').get().then((value) {
     value.docs.forEach((element) {print(element.id);});
  });

I need the doc.id for each document in a list.

The solution is

  Future<List<dynamic>> getDevices() async {
    var firebaseUser = FirebaseAuth.instance.currentUser?.email;
    var query = await firestoreInstance
        .collection(devices)
        .where('email', isEqualTo: '$firebaseUser')
        .get();

    List<String> _documentsIds = query.docs.map((doc) => doc.id).toList();
    return _documentsIds;
  }

The second part of my question were as how to use the Future, it’s as

Container(
                    width: 380,
                    padding: const EdgeInsets.symmetric(horizontal: 24.0),
                    decoration: BoxDecoration(
                        border: Border.all(
                            color: Colors.lightBlueAccent, width: 1.0),
                        borderRadius: kBorderRadius),
                    child: DropdownButtonHideUnderline(
                        child: FutureBuilder<List>(
                            future: getDevices(),
                            builder: (BuildContext context,
                                AsyncSnapshot<List<dynamic>> snapshot) {
                              if (!snapshot.hasData) {
                                return const Text(
                                  'Waiting Devices',
                                );
                              } else {
                                return DropdownButton(
                                  borderRadius: kBorderRadius,
                                  iconSize: 40,
                                  elevation: 16,
                                  hint: const Text(
                                    'Safegaurd Devices',
                                  ),
                                  onChanged: (value) {
                                    setState(() {
                                      selectedDeviceValue = value.toString();
                                      setState(() {
                                        selectedDeviceValue;
                                        getDeviceSettings();
                                      });
                                    });
                                  },
                                  value: selectedDeviceValue,
                                  items: snapshot.data
                                      ?.map((_documentsIds) =>
                                          DropdownMenuItem<String>(
                                            value: _documentsIds,
                                            child: Text(_documentsIds),
                                          ))
                                      .toList(),
                                );
                              }
                            })),
                  )

2

Answers


  1. you can do something like:

    void getDevices () async {
      var firebaseUser = FirebaseAuth.instance.currentUser?.email;
      print('firebaseUser: $firebaseUser');
     var query = await firestoreInstance.collection(devices).where('email', isEqualTo: ' 
     $firebaseUser').get();
     List documentsIds = query.docs.map((doc)=>doc.id); 
     });
    
    Login or Signup to reply.
  2. To summarise the solution from the comments:

    
    void getDevices () async {
      var firebaseUser = FirebaseAuth.instance
        .currentUser?.email;
      var snapshot =  await firestoreInstance.collection(devices)
        .where('email', isEqualTo: '$firebaseUser')
        .get();
      var idList = snapshot.docs.map(doc=> doc.id).toList();
    
    // rest of your code here
    
    }
    

    Note* when using async/await, you must use the await keyword without .then()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search