spnda / dart_minecraft

A Dart library for Minecraft and Mojang Web-APIs, authentication, and NBT Files.
https://pub.dev/packages/dart_minecraft
MIT License
20 stars 8 forks source link

can't read file #7

Closed KaanBN closed 1 year ago

KaanBN commented 1 year ago

I have the file in my folders but every time I try to read the nbt file I get error (E/flutter ( 5298): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PathNotFoundException: Cannot open file, path = 'assets/gifs/deneme1.nbt' (OS Error: No such file or directory, errno = 2))

void asd() async {
    print("executed main");
    final nbtReader = NbtReader.fromFile('assets/gifs/deneme1.nbt');
    try {
      nbtReader.read();
    } on NbtFileReadException {
      print('Failed to read data.nbt');
    }
  }
KaanBN commented 1 year ago

if anyone have the same problem as me this was my solution:

void _readFile() async 
    //I was able to extract Uint8List by doing like this.
    final ByteData data = await rootBundle.load('assets/files/dd.schem');
    //normally NbtReader.fromfile() already implemented but in my case reading bytes was not an option
   final nbtReader = NbtReader(data.buffer.asUint8List());
    // after that I was able to read file
    await nbtReader.read();
    NbtCompound<NbtTag>? rootNode = nbtReader.root;
    List<NbtTag>? block_types = rootNode?.getChildrenByName("Palette");
    print(block_types);
  }
spnda commented 1 year ago

Flutter does files a bit different depending on the platform, which is why packages like path_provider exist. NbtReader.fromFile internally just does File(path).readAsBytesSync(), which is relative to the app's working directory, which is not where the assets are stored by default afaik, though I haven't used Flutter with assets for a while. Though glad you fixed it using a different solution.