Knorst
Knorst

Reputation: 11

This expression has a type of 'void' so its value can't be used on Map

I'm new in flutter.

I'm receiving data from a DB server.

Map<String, double> dataMap = {};
var queryService = client.getQueryService();

var records = await queryService.query(query);
await records.forEach((record) {

  DateFormat dateFormat = DateFormat("HH:mm:ss");
  String formatedDate = dateFormat.format(DateTime.parse('${record['_time']}')); //Converting DateTime object to String

  dataList.add('$formatedDate : ${record['_value']}');
  dataMap[formatedDate] = double.parse('${record['_value']}');
});
print(dataMap);
client.close();

So I need to pass the dataMap data to a function:

dataMap.forEach((key, value) {
                        ChartData (key, value);
                      })

But I'm receiving the 'void' error... Any ideas?

Upvotes: 1

Views: 46

Answers (1)

jamesdlin
jamesdlin

Reputation: 90125

Iterable.forEach does not return anything. It makes no sense to await its result. Map.forEach similarly does not return anything, so you cannot pass its result as an argument to a function. Additionally, your callback does not do anything; ChartData(key, value) by itself constructs a ChartData object but then discards it.

If you want to convert dataMap to a List<ChartData>, you need to iterate over dataMap and build a List:

var chartDataList = dataMap.entries.map(
  (keyValue) => ChartData(keyValue.key, keyValue.value),
).toList();

or use collection-for:

var chartDataList = [
  for (var keyValue in dataMap.entries)
    ChartData(keyValue.key, keyValue.value),
];

Upvotes: 1

Related Questions