简体   繁体   English

错误:无法将参数类型“void Function(MidiDevice)”分配给参数类型“FutureOr”<dynamic> 函数(列表<mididevice> ?)'</mididevice></dynamic>

[英]Error: The argument type 'void Function(MidiDevice)' can't be assigned to the parameter type 'FutureOr<dynamic> Function(List<MidiDevice>?)'

I am new to Flutter and I am trying to get familiar with the flutter_midi_command package. I have an ElevatedButton and I want to print the list of available MIDI devices on click.我是 Flutter 的新手,我正在尝试熟悉 flutter_midi_command package。我有一个ElevatedButton ,我想在单击时打印可用 MIDI 设备的列表。

ElevatedButton(
  onPressed: () => MidiCommand().devices.then((MidiDevice midiDevice) => print(midiDevice)),
  child: const Text('Show available MIDI devices'),
)

But in the .then part, I am getting the following error as soon as I define the type of the returned value from the callback:但是在.then部分,一旦我定义了回调返回值的类型,我就会收到以下错误:

The argument type 'void Function(MidiDevice)' can't be assigned to the parameter type 'FutureOr<dynamic> Function(List<MidiDevice>?)'.

And but when I remove the type ( MidiDevice ), the error is removed.但是当我删除类型( MidiDevice )时,错误被删除。 I believe I am missing a core concept from Dart/Flutter.我相信我错过了 Dart/Flutter 的核心概念。 My question is, how I am able to simply make this callback function to work when I have the type of the returned value defined?我的问题是,当我定义了返回值的类型时,我如何能够简单地使这个回调 function 工作?

I managed to solve my question.我设法解决了我的问题。 The type of the returned value is not MidiDevice , its List<MidiDevice>?返回值的类型不是MidiDevice ,是List<MidiDevice>? . .

The following is the correct implementation:以下是正确的实现:

ElevatedButton(
  onPressed: () => MidiCommand().devices.then((List<MidiDevice>? midiDevice) => print(midiDevice)),
  child: const Text('Show available MIDI devices'),
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter升级后:参数类型'Future<bool> 函数(文件)'不能分配给参数类型'FutureOr<dynamic> 函数(Uint8List)'</dynamic></bool> - After Flutter upgrade: The argument type 'Future<bool> Function(File)' can't be assigned to the parameter type 'FutureOr<dynamic> Function(Uint8List)' 我得到的错误是 - Null 函数(DataSnapshot)不能分配给参数类型'FutureOr<dynamic> 函数(数据库事件)'</dynamic> - Error Im getting is - Null Function(DataSnapshot) can;t be assigned to the parameter type 'FutureOr<dynamic> Function(DatabaseEvent)' 错误:参数类型 'Future<dynamic> ' 不能分配给参数类型 'void Function()?'</dynamic> - Error: The argument type 'Future<dynamic>' can't be assigned to the parameter type 'void Function()?' 参数类型“字符串函数(字符串)”不能分配给参数类型“FutureOr”<string> 功能(电话号码)'</string> - The argument type 'String Function(String)' can't be assigned to the parameter type 'FutureOr<String> Function(PhoneNumber)' 如何解决参数类型'Future<file> 功能(地图<string, dynamic> )' 不能分配给参数类型'FutureOr<file> 在 Flutter</file></string,></file> - How to solve The argument type 'Future<File> Function(Map<String, dynamic>)' can't be assigned to the parameter type 'FutureOr<File> in Flutter 参数类型“函数?” 不能分配给参数类型“void Function()?” - The argument type 'Function?' can't be assigned to the parameter type 'void Function()?' DropdownButton 错误:无法将参数类型“void Function(String)”分配给参数类型“void Function(String?)?” - DropdownButton error: The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(String?)?' 我收到此错误:“无法将参数类型'void Function(int)'分配给参数类型'Void Function()'” - Im getting this error : "The argument type 'void Function(int)' can't be assigned to the parameter type 'Void Function()' " 参数类型&#39;Future<dynamic> 函数(字符串)”不能分配给参数类型“无效函数(字符串?)?” - The argument type 'Future<dynamic> Function(String)' can't be assigned to the parameter type 'void Function(String?)?' 参数类型“动态函数(布尔)?” 不能分配给参数类型“void Function(bool?)?” 扑 - The argument type 'dynamic Function(bool)?' can't be assigned to the parameter type 'void Function(bool?)?' Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM