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

644 views Asked by At

I would like please to turn the code below to be :

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

without having errors.

The code to be changed is here:

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']}.');
}
2

There are 2 answers

3
Zakir On BEST ANSWER

According to your code example Forex.fx() return Map<String, num>. So you have two options here.

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;
}
6
Daniel Szuta On

You have to have return statement. Edit Example:

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