I have this:
final data = [
{
'name': 'Team A',
'team': ['Klay Lewis', 'Ehsan Woodard', 'River Bains']
},
{
'name': 'Team B',
'team': ['Toyah Downs', 'Tyla Kane']
},
{
'name': 'Team C',
'team': [
'Jacky Chan',
'Yalor Rowds',
'Tim Bourla',
'Levis Strauss',
'Jane Smow'
]
}
];
I want to find the quickest way to get a sum of all team members. In the above example, it would be 3 2 5 = 10 total members.
The real world data will have thousands of teams, so quickest way needed.
CodePudding user response:
fold
will get the job done.
final int totalLength = data.fold(0, (sum, obj) => sum obj['team'].length);
CodePudding user response:
import 'package:collection/collection.dart';
const data = [...];
void main(List<String> args) {
var members = data.map((e) => (e['team'] as List).length).sum;
print(members);
}