Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving files #18

Closed
ialameh opened this issue May 19, 2020 · 2 comments
Closed

Saving files #18

ialameh opened this issue May 19, 2020 · 2 comments

Comments

@ialameh
Copy link

ialameh commented May 19, 2020

I have an issue,
I am getting the path using getExternalStorageDirectory
I have a file in an asset directory (as a template), I am reading that file

Future<ex.Excel> loadExcel(
      {@required String sheet, @required String fileName, bool isAsset = false}) async {
    File file = File(fileName);
    ByteData data;
    if (isAsset == true) {
       data = await rootBundle.load(fileName);}
      else { 
        print("arrived to read filename $fileName");
        data = file.readAsBytesSync().buffer.asByteData();
        }
    var bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    ex.Excel excel = ex.Excel.decodeBytes(bytes, update: true);
    return excel;
  }

I use rootBundle.load is from the asset and using file if it is from the app directory (through the path)

saving got no issue, using 

Future<void> saveExcelSheet(
      {@required ex.Excel excel, @required String fileName}) async {
    String dir = await getPath();
    if (await Permission.storage.request().isGranted) {
      excel.encode().then((onValue) async {
        File file = File("$dir/$fileName");
          file.createSync(recursive: true);
          file.writeAsBytesSync(onValue);
      });
    } else {
      print('permission not granted');
    }
  }
}

what I am facing, is if I initiate the file (copy it to the app directory) and try to read it immediatly, I get file not found error, even though the file exist

I thought it might be that the file needs to be closed, but I could not figure it out how to close the file.,

Any ideas?

@justkawal
Copy link
Owner

excel.encode().then((onValue) async {
        File file = File("$dir/$fileName");
          file.createSync(recursive: true);
          file.writeAsBytesSync(onValue);
      }).then((_){
      // try to read it here 
      });

@ialameh
Copy link
Author

ialameh commented May 20, 2020

That is interesting, thank you, I will try it.

I sort out my problem when I found I was making unnecessary multiple reads for the file.

I can thank you enough for the great package, I will make another donation as soon as possible, I urge everyone who use this to donate.

Thanks again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants