'Load docx file from assets in Flutter

I have a terms and conditions .docx document, If you have a crypto.com account you see in the terms and conditions they essentially just load a docx document out of somewhere. I'd like to load mine from my assets using file_reader but it takes the full path of the file, not the relative assets file.

I guess the real question is where can I store this file, so when I compile it, I can then somehow figure out the path of the file, and then feed it to the FileReaderView.



Solution 1:[1]

Try to use: open_file. It may solve your problem, they use a relative path.

Solution 2:[2]

Future<File> openFileFromAsset() async {
    Directory tempDir = await getTemporaryDirectory();
    File tempFile = File('${tempDir.path}/set_any_name.docx');
    ByteData bd = await rootBundle.load('assets/en/legal_notes.docx');
    await tempFile.writeAsBytes(bd.buffer.asUint8List(), flush: true);
    return tempFile;
  }
openFileFromAsset().then((file){OpenFile.open(file.path);});

Also update your pubspec.yaml file:

flutter:
  assets:
    - assets/files/samplefile.docx

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ProDevUser
Solution 2 ProDevUser