3

map_repository

     Future<MapModel> selectedMapJson(String searchMapName) async {
    List<MapModel> mapList = [];
    List<MapModel> selectedMapList = [];
    MapModel selectedMap;

    Directory directory = await getApplicationDocumentsDirectory();
    File file = File("${directory.path}/map_data.json");

    var contents = await json.decode(file.readAsStringSync());

    contents.forEach((e, v) {
      Map<String, dynamic> content = {e: v};
      Map<String, dynamic> data = content[e];

      mapList.add(MapModel.fromJson(data));
    });

    selectedMapList =
        mapList.where((e) => e.name!.contains(searchMapName)).toList();

    selectedMap = selectedMapList[0];

    return Future.value(selectedMap);
  }

tech_tree_repository

    Future<dynamic> selectedTechTreeJson(
      selectedTribe, selectedTechTreeName) async {
    List techTreeList = [];
    late List selectedtechTreeList;
    late var selectedTechTree;
    late var techTreeContents;
    ReadFile readFile = new ReadFile();

    final Directory directory = await getApplicationDocumentsDirectory();
    late File file;

    if (selectedTribe == "terran") {
      file = File("${directory.path}/terran_tech_tree.json");
      techTreeContents = await json.decode(file.readAsStringSync());
    } else if (selectedTribe == "protoss") {
      file = File("${directory.path}/protoss_tech_tree.json");
      techTreeContents = await json.decode(file.readAsStringSync());
    } else {
      file = File("${directory.path}/zerg_tech_tree.json");
      techTreeContents = await json.decode(file.readAsStringSync());
    }

    techTreeContents.forEach((e, v) {
      Map<String, dynamic> content = {e: v};
      Map<String, dynamic> data = content[e];

      techTreeList.add(TechTreeModel.fromJson(data));
    });

    selectedtechTreeList = techTreeList
        .where((e) => e.name.contains(selectedTechTreeName))
        .toList();

    selectedTechTree = selectedtechTreeList[0];

    return Future.value(selectedTechTree);
  }

Why does the map_repository work, but why does the tech_tree_repository not work? OS Error: No push file or directory, erno = 2 is only available when tech_tree_repository is operated.

  1. Aset folder verification completed (no problem).
  2. File name resolution complete (no problem).

I'm in a hurry. Please help me.

a.ak
  • 659
  • 2
  • 12
  • 26
97_HYUN
  • 83
  • 2
  • 6
  • you can checkout this post : > https://stackoverflow.com/questions/57884286/os-error-no-such-file-or-directory-errno-2 – Amede Angel Aulerien Sep 06 '21 at 07:36
  • Actually, I've already used rootBundle, but my repository is to make a like button for the content, and then click on the like button and use selectedTechTreeJson. There is no update on the click of the button click. – 97_HYUN Sep 06 '21 at 07:41
  • I wonder why the map_repository runs without problems, but the tech_tree_repository does not. – 97_HYUN Sep 06 '21 at 07:43

1 Answers1

0

The error no such file or directory, errno = 2 is usually thrown when the app is unable to access the path set in File() because it's nowhere to be found. One way of ensuring all things are fine is by using permission_handler to manage storage read/write access. Then wrap the file access functions with Future.sync() to fetch any errors being thrown.

Future<int> selectedTechTreeJson(
      selectedTribe, selectedTechTreeName) async {
  return Future.sync(() {
    // Read file here
  });
}
Omatt
  • 8,564
  • 2
  • 42
  • 144