I am getting a random value from some API, and I have a condition for it. If the condition is true, I will return a widget, else I want to change the random value and get another random value from the API again.
I need to rebuild just the FutureBuilder
widget, and I dont know how to do this, I got an error while using setState
which is setState() or markNeedsBuild() called during build.
This is my code so far:
FutureBuilder<List<dynamic>>(
future: API.get_pets(randomly_select_URL()),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<dynamic>? pet_data = snapshot.data;
if (dropDownIsSelected == true) {
var number_of_parameters = snapshot.data!.length;
var random_pet = random.nextInt(number_of_parameters);
var category =
pet_data![random_pet].category.toString();
var photoURL =
pet_data![random_pet].photoUrls.toString();
if (notEqualsIgnoreCase(category, "Kitty") ||
notEqualsIgnoreCase(category, "Puppy") ||
notEqualsIgnoreCase(category, "Fish") ||
notEqualsIgnoreCase(category, "Hedgehog") ||
notEqualsIgnoreCase(category, "Bunny") ||
photoURL.length == 0) {
print("NOT IN CATEGORY");
random_pet = random.nextInt(number_of_parameters);
}
else {
return Random_Card();
}
}
future: API.get_pets(randomly_select_URL())
Don't build the future as the future: parameter in FutureBuilder. See the first few paragraphs of the FutureBuilder documentation, or see my video at youtube.com/watch?v=sqE-J8YJnpg.