0

I've been working with flutter-hooks' useFuture method, but I need to use it conditionally (based on user-input in this case). I've found this approach, but I need to know if there is a better way:

Storing what data is selected using useState, and if there is some data selected, change the future from null to the actual future. Example code:

final selected = useState<int>(null);
final future = useMemoizedFuture(() => selected.value != null ? http.get("someapi") : null);
teslafan0
  • 25
  • 4

1 Answers1

0

Use the data as the keys of the memoizer. Then add the conditions for working with the future in the code where you need it, not in the hooks themselves.

final selectedIndex = useState<int?>(null);
final someFuture = useMemoizedFuture(() => http.get("someapi"), [useSelected.value]);
Agon Noga
  • 563
  • 1
  • 9
  • 17