我使用以下代码打印列表
print(snapshot.data.toList().toString());
,这里是输出[ABC(oid: 343, id: 138), ABC(oid: 344, id: 139),ABC(oid:345,id:138)]
我尝试使用软件包collection根据ID对列表进行分组:
var _reading = snapshot.data[index];
var newMap = groupBy(snapshot.data.toList(),
(obj) => obj[_reading.id]);
错误
类'ABC'没有实例方法'[]'。 接收方:“ ABC”实例尝试调用:
[](12)
答案 0 :(得分:0)
也许这个简单的例子对您有帮助?
import 'package:enumerable/enumerable.dart';
void main() {
final query = data.groupBy((e) => e.id);
print(query);
print(query.toMap((e) => e.key));
for (final element in query) {
print('key: ${element.key}');
for (var value in element) {
print(' $value');
}
}
}
final data = [
ABC(oid: 343, id: 138),
ABC(oid: 344, id: 139),
ABC(oid: 345, id: 138)
];
class ABC {
int id;
int oid;
@override
String toString() {
return 'ABC <oid: $oid, id: $id>';
}
ABC({this.id, this.oid});
}
结果:
((ABC <oid: 343, id: 138>, ABC <oid: 345, id: 138>), (ABC <oid: 344, id: 139>))
{138: (ABC <oid: 343, id: 138>, ABC <oid: 345, id: 138>), 139: (ABC <oid: 344, id: 139>)}
key: 138
ABC <oid: 343, id: 138>
ABC <oid: 345, id: 138>
key: 139
ABC <oid: 344, id: 139>