0

I am trying to show a list of items in a DropdownField, which requires a non-future list.

So I am trying to turn my future list (which I get from a database call) into a normal list by iterating over it and adding it to a new, normal, list, like so; enter image description here enter image description here

When I print the individual elements of the future list, I get results back.

enter image description here

But afterwards, when I print the new list, it returns as a null value.

enter image description here

Does anyone have any idea as to how I can solve this issue ?

PS: I am NOT interested in a FutureBuilder solution. I know that I can show a future list with a futurebuilder, but this will not fit the solution I am hoping to achieve.

Thanks in advance!

lvb
  • 41
  • 3
  • For future reference, you might want to paste your actual code into the question so people can easily copy it into their answers. Otherwise, we have to write it out ourselves. – Caleb Robinson Feb 02 '21 at 17:33

2 Answers2

1

The problem is that you are not initializing newList. You are trying to add items to a null list. Change the fourth line of getAll() to the following:

var newList = List<dynamic>();

You are also not awaiting your firebase call. This means that you will print the array BEFORE you finish your firebase call. You need to make this getAll() method async and await, the firebase call so it doesn't print the null version.

Caleb Robinson
  • 1,010
  • 13
  • 21
1

That’s because you’re not waiting for the future to complete. When you do print(newList) the future isn’t completed yet.

You will have to need to await the getAnswers data to archieve what you want.

final answers = await getAnswersList();
for (final answer in answers) {
  print(answer.toString());
}
Stijn2210
  • 812
  • 6
  • 14