如何在 dart flutter 中将 Future<void>main 转换为 Future<List> 而不会出错

how to trun a Future<void>main to a Future<List> in dart flutter without errors

我想请把下面的代码变成:

Future<List> fetchPrices() async {details in code below} 

没有错误。

要更改的代码在这里:

Future<void> main(List<String> arguments) async {
  Map<String, num> quotes = await Forex.fx(
    quoteProvider: QuoteProvider.yahoo,
    base: 'USD',
    quotes: <String>['EUR', 'JPY']
  );
  print('Number of quotes retrieved: ${quotes.keys.length}.');
  print('Exchange rate USDEUR: ${quotes['USDEUR']}.');
  print('Exchange rate USDJPY: ${quotes['USDJPY']}.');
  quotes = await Forex.fx(
    quoteProvider: QuoteProvider.ecb,
    base: 'JPY',
    quotes: <String>['EUR', 'USD']
  );
  print('Number of quotes retrieved: ${quotes.keys.length}.');
  print('Exchange rate JPYEUR: ${quotes['JPYEUR']}.');
  print('Exchange rate JPYUSD: ${quotes['JPYUSD']}.');
}

你必须有return声明。 编辑 示例:

Future<List> fooFunction() async {
    Map<Bar, Baz> data = await someAsyncFunction();
    List<String> strings = List();
    strings.add(data.value);
    [...]
    return strings;
}

根据您的代码示例 Forex.fx() return Map<String, num>。所以你在这里有两个选择。

Future<List<String>> fooFunction(List<String> arguments) async {
  Map<String, num> quotes = await Forex.fx(
      quoteProvider: QuoteProvider.yahoo,
      base: 'USD',
      quotes: <String>['EUR', 'JPY']
  );
  return quotes.keys;
}

Future<List<num>> fooFunction(List<String> arguments) async {
  Map<String, num> quotes = await Forex.fx(
      quoteProvider: QuoteProvider.yahoo,
      base: 'USD',
      quotes: <String>['EUR', 'JPY']
  );
  return quotes.values;
}