0

I need to use my list from Firestore, but I'am stuck how to convert my Future list which I get from Firestore to list of strings:

Future<List> getListOfProducts() async {
    List<String> productsList;
    final List<DocumentSnapshot> documents =
        (await FirebaseFirestore.instance.collection('products').get()).docs;
    productsList = documents
        .map((documentSnapshot) => documentSnapshot['productName'] as String)
        .toList();
    return productsList;
  }

What should I do with productList now ?

Quqlock
  • 13
  • 3

1 Answers1

1

Since this function is async, it must return a Future. However what you can do is await the result when calling the function. List<String> lst = await getListOfProducts()

Abbas.M
  • 3,086
  • 3
  • 11
  • 22
  • I tried to do this, but I got "The await expression can only be used in an async function." error. – Quqlock Oct 06 '20 at 08:59
  • That means that you are using the await keyword illegally in a place you cant. How/where are you using it? – Abbas.M Oct 06 '20 at 09:02
  • here is my code: List myProductsList = await getListOfProducts(); – Quqlock Oct 06 '20 at 09:04
  • I meant where are you using that? Are you using it inside a function that's passed to the `onPressed` of a button? Are you using it in a constructor? etc. – Abbas.M Oct 06 '20 at 09:25
  • all is clear now, I forgot about async in my main function, thank You – Quqlock Oct 06 '20 at 10:19