Home > Mobile >  ''List<Object?>' is not a subtype of type 'Uint8List?'' on IOS F
''List<Object?>' is not a subtype of type 'Uint8List?'' on IOS F

Time:12-12

NOTE: Please do not do a knee-jerk close recommendation based on "more code required for a minimal reproducible example" especially if you don't understand the question. If you follow my logic, I think you will see that more code is not required.

I'm doing some platform specific Flutter code where I have a platform method "stopRec" (stop recording) which awaits a byte array from the native host.

On the Dart side, it looks like this:

Uint8List? recordedBytes;
recordedBytes = await platform.invokeMethod('stopRec');

As you can see it's expecting to get a byte array (Dart Uint8List) back.

I've written the Android code and it works -- it tests out fine, the recorded bytes come back through and playback correctly.

This is what the Android (Java) code looks like:

byte[] soundBytes = recorder.getRecordedBytes();
result.success(soundBytes);

I hope you understand why "more code" is not yet necessary in this question.

Continuing, though, on the IOS side, I'm getting the following error when calling the platform method:

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type 'List<Object?>' is not a subtype of type 'Uint8List?' in type cast

The Dart line where the error occurs is:

recordedBytes = await platform.invokeMethod('stopRec'); 

So what is happening is that it's not getting a the Dart Uint8List it expects sent back from IOS.

The IOS code looks like this:

var dartCompatibleAudioBytes:[UInt8]?
var audioBytesAsDataFromRecorder: Data?

// ..... platform channel section 
    case "stopRec":
        self?.stopRec()
        result(self?.dartCompatibleAudioBytes!) // <---- wrong data type getting sent back here
        break
// ..... platform channel section 


private func stopRec() {
    myRecorder.stopRecording()
    audioBytesAsDataFromRecorder = myRecorder.getRecordedAudioFileBytesAsData()
    dartCompatibleAudioBytes = [UInt8] (audioBytesAsDataFromRecorder!)
}

I have tested the same IOS implementation code as a stand-alone IOS app that is not connected to Flutter, so I know that at the end of the the stopRec() method, the dartCompatibleAudioBytes variable does contain the expected data which plays back properly.

I hope you can see why "more code" is still not necessary.

  • The Dart code works
  • The Android code Works
  • The Dart code works together with the Android Code
  • The IOS code works
  • The IOS code does NOT work together with the Dart code

Using what I've shown, can anyone see immediately why the expected data type is not making its way back through the method channel?

CodePudding user response:

According to the documentation, you should be using FlutterStandardTypedData(bytes: Data) in swift in order for it to be deserialized as Uint8List in dart.

  • Related