I have the function below where i call another async function "getAnimeById( )".
Whenever I hot restart my app, i get the file exception and the set that is loaded has duplicates.
Future<void> fetchRecommendations() async {
//grab data from local disk
LocalStorage storage = LocalStorage("recommended_data");
await storage.ready;
//clear the set to avoid showing anime that arent in the list anymore
_recommendedSet.clear();
Map tempMap;
//try to fetch data, if not available create it
try {
tempMap = await storage.getItem("recommendedMap");
} catch (error) {
tempMap = {};
}
for (var element in tempMap.keys) {
if (!animeData.containsKey(element)) {
await getAnimeById(element);
}
final tempAnime = animeData[element]!;
checkIndex += 1;
_recommendedSet.add(tempAnime);
}
notifyListeners();
}
Future<void> getAnimeById(String id) async {
....
LocalStorage storage = LocalStorage("episode_data");
await storage.ready;
Map<String, dynamic> episodeMap = {};
// if the episode data does not exist we continue with an empty map
try {
episodeMap = storage.getItem("episodeMap");
} catch (error) {
episodeMap = {};
}
.....
try {
await storage
.setItem("episodeMap", episodeMap) //This is the line that throws the error
.then((value) => storage.dispose());
} catch (error) {
print(error);
}
}
The above code works fine after the home screen is pushed again on the navigator stack. It only happens when the app is started fresh.
I also found that the error happens only for certain indexes in the loop in the first function.