How to deal with Flutter Local Storage gives a File Exception an async operation is currently pending

0 votes

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.

Apr 18, 2023 in Flutter by Ashwini
• 5,430 points
1,617 views

1 answer to this question.

0 votes

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.

answered Apr 18, 2023 by Anitha

Related Questions In Flutter

0 votes
1 answer

How to save to local storage using Flutter?

Yes, Flutter has a built-in mechanism for ...READ MORE

answered Mar 28, 2023 in Flutter by Tej
2,300 views
0 votes
1 answer

How to make a TextField with HintText but no Underline?

You can achieve this by using the ...READ MORE

answered Mar 21, 2023 in Flutter by Anushi
6,828 views
0 votes
1 answer

How to dismiss an AlertDialog on a FlatButton click?

To dismiss the AlertDialog when the FlatButton ...READ MORE

answered Mar 26, 2023 in Flutter by pooja
2,757 views
0 votes
1 answer

Flutter, run async function on stream.listen

You can make the listen callback an ...READ MORE

answered Mar 20, 2023 in Android by pooja
1,366 views
0 votes
1 answer

What is the future of flutter?

Hi@MD, Flutter is a rather new cross-platform framework ...READ MORE

answered Jul 17, 2020 in Others by akhtar
• 38,260 points
1,147 views
0 votes
1 answer

What is Flutter?

Hi@akhtar, Flutter is an app SDK for building ...READ MORE

answered Jul 17, 2020 in Others by MD
• 95,460 points
1,450 views
0 votes
2 answers

How to create a circle icon button in Flutter?

To create something similar to a FloatingActionButton, ...READ MORE

answered Aug 23, 2023 in Flutter by anonymous
• 140 points
14,354 views
0 votes
1 answer

How to Deserialize a list of objects from json in flutter Ask Question?

To deserialize a list of objects from ...READ MORE

answered Mar 28, 2023 in Flutter by vishalini
2,281 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP