'List<Object?>' 不是 'Uint8List?' 类型的子类型,在 IOS Flutter Platform Channel 上
''List<Object?>' is not a subtype of type 'Uint8List?'' on IOS Flutter Platform Channel
注意:请不要根据“最小可重现示例需要更多代码”做出下意识的关闭建议,尤其是在您不理解问题的情况下。如果您遵循我的逻辑,我想您会发现不需要更多代码。
我正在做一些平台特定的 Flutter 代码,其中我有一个平台方法“stopRec”(停止记录)等待来自本机主机的字节数组。
在 Dart 方面,它看起来像这样:
Uint8List? recordedBytes;
recordedBytes = await platform.invokeMethod('stopRec');
如您所见,它期望返回一个字节数组 (Dart Uint8List)。
我已经编写了 Android 代码 并且它可以工作 -- 它测试正常,记录的字节返回并正确播放。
这就是 Android (Java) 代码的样子:
byte[] soundBytes = recorder.getRecordedBytes();
result.success(soundBytes);
我希望你明白为什么在这个问题中还不需要“更多代码”。
不过,在 IOS 方面,调用平台方法时出现以下错误:
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type
'List<Object?>' is not a subtype of type 'Uint8List?' in type cast
出现错误的Dart行是:
recordedBytes = await platform.invokeMethod('stopRec');
所以发生的事情是它没有得到它期望从 IOS 发回的 Dart Uint8List。
IOS 代码如下所示:
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!)
}
我测试了与未连接到 Flutter 的独立 IOS 应用程序相同的 IOS 实现代码,因此我知道在 stopRec() 方法的末尾, dartCompatibleAudioBytes 变量 确实 包含可以正常播放的预期数据。
我希望你能明白为什么仍然不需要“更多代码”。
- Dart 代码有效
- Android 代码有效
- Dart 代码与 Android 代码一起工作
- IOS 代码有效
- IOS 代码不能与 Dart 代码一起工作
使用我展示的内容,任何人都可以立即看出为什么预期的数据类型没有通过方法通道返回吗?
根据 documentation,您应该在 swift 中使用 FlutterStandardTypedData(bytes: Data)
,以便在 dart 中将其反序列化为 Uint8List
。
注意:请不要根据“最小可重现示例需要更多代码”做出下意识的关闭建议,尤其是在您不理解问题的情况下。如果您遵循我的逻辑,我想您会发现不需要更多代码。
我正在做一些平台特定的 Flutter 代码,其中我有一个平台方法“stopRec”(停止记录)等待来自本机主机的字节数组。
在 Dart 方面,它看起来像这样:
Uint8List? recordedBytes;
recordedBytes = await platform.invokeMethod('stopRec');
如您所见,它期望返回一个字节数组 (Dart Uint8List)。
我已经编写了 Android 代码 并且它可以工作 -- 它测试正常,记录的字节返回并正确播放。
这就是 Android (Java) 代码的样子:
byte[] soundBytes = recorder.getRecordedBytes();
result.success(soundBytes);
我希望你明白为什么在这个问题中还不需要“更多代码”。
不过,在 IOS 方面,调用平台方法时出现以下错误:
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type 'List<Object?>' is not a subtype of type 'Uint8List?' in type cast
出现错误的Dart行是:
recordedBytes = await platform.invokeMethod('stopRec');
所以发生的事情是它没有得到它期望从 IOS 发回的 Dart Uint8List。
IOS 代码如下所示:
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!)
}
我测试了与未连接到 Flutter 的独立 IOS 应用程序相同的 IOS 实现代码,因此我知道在 stopRec() 方法的末尾, dartCompatibleAudioBytes 变量 确实 包含可以正常播放的预期数据。
我希望你能明白为什么仍然不需要“更多代码”。
- Dart 代码有效
- Android 代码有效
- Dart 代码与 Android 代码一起工作
- IOS 代码有效
- IOS 代码不能与 Dart 代码一起工作
使用我展示的内容,任何人都可以立即看出为什么预期的数据类型没有通过方法通道返回吗?
根据 documentation,您应该在 swift 中使用 FlutterStandardTypedData(bytes: Data)
,以便在 dart 中将其反序列化为 Uint8List
。