miguelpruivo/flutter_file_picker

type 'NativeByteBuffer' is not a subtype of type 'List<int>'

Closed this issue · 5 comments

Problem occurred with new version of file_picker (8.0.1). When trying to send file provided by FilePicker.platform.pickFiles on WEB platform i get error:

TypeError: Instance of 'NativeByteBuffer': type 'NativeByteBuffer' is not a subtype of type 'List<int>'

Problem does not exist on version 6.2.1.

My code:

final results = await FilePicker.platform.pickFiles(
  allowMultiple: false,
  type: FileType.custom,
  allowedExtensions: ['jpg', 'jpeg', 'png'],
  withData: false,
  withReadStream: true,
);
 
if (results == null || results.files.isEmpty) return false;
 
final file = results.files.first;
 
if (file != null) {
  ApiMultipartRequest request = ApiMultipartRequest(method, uri);
  
  final fileReadStream = file.readStream;
  if (fileReadStream != null) {
    final stream = http.ByteStream(fileReadStream);
    request.files.add(
      http.MultipartFile(
        'files',
        stream,
        platformFile.size,
        filename: platformFile.name,
      ),
    );
  }
}

Flutter Version details

[✓] Flutter (Channel stable, 3.19.6, on Ubuntu 20.04.3 LTS 5.15.0-105-generic, locale pl_PL.UTF-8)
    • Flutter version 3.19.6 on channel stable at /home/kacper/snap/flutter/common/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 54e66469a9 (7 dni temu), 2024-04-17 13:08:03 -0700
    • Engine revision c4cd48e186
    • Dart version 3.3.4
    • DevTools version 2.31.1

[!] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /home/kacper/Android/Sdk
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/linux#android-setup for more details.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • clang version 10.0.0-4ubuntu1
    • cmake version 3.16.3
    • ninja version 1.10.0
    • pkg-config version 0.29.1

[✓] Android Studio (version 2022.2)
    • Android Studio at /home/kacper/android-studio-2021.2.1.16-linux/android-studio
    • Flutter plugin version 75.1.1
    • Dart plugin version 222.4582
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Ubuntu 20.04.3 LTS 5.15.0-105-generic
    • Chrome (web)    • chrome • web-javascript • Google Chrome 118.0.5993.70

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

I have exactly the same problem on the web part.

I'm trying to find a solution on my side.

Okay I'm back with a solution

Already down to version 6.2.1 because apparently there is a typing error in the file stream
All execution must be in the same method, processing the file and sending the file, the web part of flutter is really very capricious, separated it is two methods would only lose the integrity of the result file by a phenomenon which surpasses me
I had to build a class interface to separate the web part and the app part since the web part is allergic to dart.io

Exemple my code for web :

`
import "package:http/http.dart" as http;
Future setFile(FilePickerResult? result, String typeUpload, String fileUrl) async {
try {

  // if file
  if (result != null && result.files.isNotEmpty && result.files.single.readStream != null) {
    
   // Uri address
    Uri uri = Uri.parse(_urlList.urlPostUploadFile);

    /// array post 
    Map<String, String> requestMap = {
      "cible": typeUpload,
    };

    /// prepare to request file
    /// read stream ??? WTF type File ???? baaa it works with this ;)
    http.ByteStream stream = http.ByteStream(result.files.single.readStream!.asBroadcastStream());

    /// Include multiparte file
    var request = http.MultipartRequest('POST', uri);
    request.fields.addAll(requestMap);
    request.files.add(http.MultipartFile(
      'file',
      stream,
      result.files.first.size,
      filename: result.files.first.name,
    ));

    /// Post file
    var response = await request.send();
    if (response.statusCode == 200) {
      /// Lecture des données - retour serveur
      String responseString = await response.stream.bytesToString();

      print("REPONSE : $responseString");

      /// Décodage des données json
      var jsonData = jsonDecode(responseString);
      _setInfo = CtrlUploadData.fromJson(jsonData);

      /// check code info
      if (_setInfo.returnCode != 0 && _setInfo.returnCode <= 8) {
        return false;
      } else {
        return true;
      }
    } else {
      return false;
    }
  } else {
    return false;
  }
} catch (ex) {
  return false;
}

`

We really need to make an effort to clarify and enter into a standardized convention, the backtracking in this part of the code has been numerous on my side and the doc is not at all in line with what is happening concretely.

I spent some time on this and made the effort to detail everything, I hope this will be taken into account in the next patch

Sorry but it's frustrating for me to see this error several times

in particular it is the typing problem of result.files.first.readStream which poses recurring problems.
More info on this and clear documentation would not be refused.

Sorry for my English, best regards

I would at least like to have a response from you before you close this thread ?!
Best regards

See the fix in #1496