我正在決議一個 JSON 陣列,Iterable<Dto>例如:
final Iterable l = json.decode(resp.body);
Iterable<Dto> dtos = l.map((data) => Dto.fromJson(data));
但是_TypeError (type 'MappedListIterable<dynamic, dynamic>' is not a subtype of type 'Iterable<Dto>')當我們將兩個陳述句鏈接起來時會導致:
Iterable<Dto> dtos = json
.decode(resp.body)
.map((data) => Dto.fromJson(data));
請注意,這是重構操作的結果Inline Local Variable。
那么,我們應該如何正確重構“行內區域變數”?
uj5u.com熱心網友回復:
您可以嘗試以下幾行;
Iterable<Dto> dtos = (json.decode(resp.body) as Iterable)
.map((data) => Dto.fromJson(data));
uj5u.com熱心網友回復:
您還可以將它們作為串列獲取:
var l = json.decode(resp.body);
List<Dto> dtos = l.map((data) => Dto.fromJson(data)).toList();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/388151.html
下一篇:引數型別'List<Todo>?Function(QuerySnapshot<Object?>)'不能分配給引數型別'List<Todo&g
