我有这种方法,可以在Dart 2中进行编译,但是在运行时出现以下错误
类型“
List<dynamic>”不是类型的子类型“List<ExchangeRate>”
如您在代码中所见,我在其中创建并返回了新的ExchangeRate对象.map(),然后返回了rateEntries.toList()我希望是type的a List<ExchangeRate>,但是似乎可以推断为type List<dynamic>!
@override
Future<List<ExchangeRate>> getExchangeRatesAt(DateTime time, Currency baseCurrency) async {
final http.Client client = http.Client();
final String uri = "some uri ...";
return await client
.get(uri)
.then((response) {
var jsonEntries = json.decode(response.body) as Map<String, dynamic>;
var rateJsonEntries = jsonEntries["rates"].entries.toList();
var rateEntries = rateJsonEntries.map((x) {
return new ExchangeRate(x.value.toDouble());
});
return rateEntries.toList(); // WHY IS IT RETURNING A List<dynamic> here?
})
.catchError((e) => print(e))
.whenComplete(() => client.close());
} …Run Code Online (Sandbox Code Playgroud) 在 Dart (Flutter) 中,我可以一次性获得 List 中满足条件的第一项吗?
目前,我获得满足条件的列表中的第一项的方式如下 - 即 1) 执行“where” 2) 然后请求“first”:
List<Currency> currencies = ...;
Currency dollar = currencies.where((currency) => currency.code == "USD").first;
Run Code Online (Sandbox Code Playgroud)
它不接受类似的东西:
Currency dollar = currencies.first((currency) => currency.code == "USD");
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
dart ×2