The error "a file exception an async operation is currently pending" occurs when an asynchronous operation is still in progress when another operation is called. In this case, it seems that the setItem operation in the getAnimeById function is still in progress when the fetchRecommendations function is called.
To fix this, you can try using await instead of chaining then in the setItem operation, like this:
await storage.setItem("episodeMap", episodeMap);
storage.dispose();
This ensures that the setItem operation is completed before disposing of the storage object.
Alternatively, you can also try using the await keyword when calling the getAnimeById function inside the loop in the fetchRecommendations function. This will ensure that each call to getAnimeById is completed before moving on to the next element in the loop.
for (var element in tempMap.keys) {
if (!animeData.containsKey(element)) {
await getAnimeById(element);
}
final tempAnime = animeData[element]!;
checkIndex += 1;
_recommendedSet.add(tempAnime);
}
By doing this, you can ensure that all asynchronous operations are completed before moving on to the next operation, and thus prevent the "async operation is currently pending" error.
To know more, join our Flutter Certification today.