0

I'm using dart:io to convert file to bytes. with image and short video it's work. but with large file (more than 1GB) i got error

E/flutter (16030): [ERROR:flutter/shell/common/shell.cc(93)] Dart Error: NewExternalTypedData expects argument 'length' to be in the range [0..1073741823].

my code

Uint8List bytes;
          try {
            bytes = imageFile.readAsBytesSync();
            print('bytes $bytes');
            lengthInBytes = bytes.buffer.asByteData().lengthInBytes;
          } catch (e) {
            print('e $e');
          }

it's look like limit of memory. any way to solve this?

7
  • Why are you doing that? What is the use case you are trying to accomplish? Commented Aug 4, 2022 at 4:02
  • i'm try to get lengthInBytes to upload from phone to my api. this is a one param Commented Aug 4, 2022 at 4:05
  • how are you uploading your data? Commented Aug 4, 2022 at 4:18
  • 1
    You are not converting a file to bytes. You are just trying to place the bytes of a file in memory. If you only want to know file size then that is a bad idea. The File class can simply tell you the size of the file. Commented Aug 4, 2022 at 4:47
  • I have uploaded the whole file Commented Aug 4, 2022 at 7:11

2 Answers 2

1

Try uploading in chunks of the file size is bigger. You can try some package like https://pub.dev/packages/chunked_uploader

ChunkedUploader chunkedUploader = ChunkedUploader(Dio(BaseOptions(
    baseUrl: 'https://example.com/api',
    headers: {'Authorization': 'Bearer'})));
try {
  Response? response = await chunkedUploader.upload(
      filePath: '/path/to/file',
      maxChunkSize: 500000,
      path: '/file',
      onUploadProgress: (progress) => print(progress));
  print(response);
} on DioError catch (e) {
  print(e);
}

It will automatically convert into chunks and upload

Sign up to request clarification or add additional context in comments.

11 Comments

there is no no need to use any non standard package: ordinary http package and MultipartFile is good enough
Only if the range is between 0..1073741823
what range? you mean the uploaded file size? where did you get that limit from?
In the error message mentioned by the op
the error says there is no enough memory to keep the whole file content, it does not come from the uploading process
|
0

i found a solution to get file size

imageFile.lengthSync()

it's solve my problem. readAsBytesSync make full memory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.