错误:无法将参数类型'void Function(MidiDevice)'分配给参数类型"未来或<dynamic>函数(列表<MidiDevice>)?



我是新的扑动,我正试图熟悉flutter_mid_command包。我有一个ElevatedButton,我想在点击时打印可用的MIDI设备列表。

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

但在.then部分,我得到以下错误,只要我从回调中定义返回值的类型:

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

但是当我删除类型(MidiDevice)时,错误被删除。我相信我错过了Dart/Flutter的一个核心概念。我的问题是,当我定义了返回值的类型时,我如何能够简单地使这个回调函数工作?

我设法解决了我的问题。返回值的类型不是MidiDevice,而是List<MidiDevice>?

正确的实现如下:

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

相关内容

最新更新