[英]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.