【问题标题】:A value of type 'Iterable<Transactions>' can't be returned from the function '_recentTransactions' because it has a return type of 'List<Transactions>无法从函数“_recentTransactions”返回类型为“Iterable<Transactions>”的值,因为它的返回类型为“List<Transactions>”
【发布时间】:2021-10-05 05:52:16
【问题描述】:
当我添加 where 函数时,我的 dart 代码出现问题,它给了我这个错误 函数“_recentTransactions”无法返回“Iterable”类型的值,因为它的返回类型为“List”
final List<Transactions> _transactionsList = [
];
List<Transactions> get _recentTransactions{
return _transactionsList.where((element) => {
};
}
【问题讨论】:
标签:
android
flutter
dart
flutter-layout
flutter-animation
【解决方案1】:
只需调用 toList() 它在 Iterable 类中:
List<Transactions> get recentTransactions {
return _transactionsList.where((element) {
return true // or whatever;
}).toList();
}
【解决方案2】:
尝试将您的返回值转换为List<Transactions>:
List<Transactions> get recentTransactions {
return _transactionsList.where((element) {
//...
}) as List<Transactions>;
}