解决Dart中嵌套列表转扁平列表的问题
针对你的代码修改
原代码里,invoice.map((e) => e.products)返回的是Iterable<List<Product>>,转成List后就变成了List<List<Product>>,不符合你需要的返回类型。直接用expand方法就能把所有子列表的元素平铺开,修改后的代码如下:
Future<List<Product>> getproduct() async {
final invoice = await isar.invoices.where().findAll();
// expand会把每个invoice的products元素展开成连续的Iterable
final p = invoice.expand((e) => e.products).toList();
return p;
}
Dart中List<List