I'm encountering an error ('type 'Null' is not a subtype of type 'List<dynamic>') when trying to extract an array of integers from a JSON object for use in a bar chart. I assume that this means the list contains null values. I have a custom function to aggregate my data:
dynamic calculateFrequenciesForProject(
CategoriesStruct metrics,
List<dynamic> measurements,
) {
/// MODIFY CODE ONLY BELOW THIS LINE
// Initialize frequency map for each question
Map<String, List<int>> frequencyMap = {};
metrics.toMap().forEach((question, options) {
frequencyMap[question] = List<int>.filled(options!.length, 0);
});
// Loop through each measurement
for (var measurement in measurements) {
var currentMeasurement = measurement;
// If measurement is a JSON string, decode it to a map
if (currentMeasurement is String) {
currentMeasurement = jsonDecode(currentMeasurement);
}
// Loop through each question
metrics.toMap().forEach((question, options) {
var answer = currentMeasurement[question];
// Increment frequency if answer exists and is valid
if (answer != null) {
var index = options!.indexOf(answer);
if (index != -1) {
frequencyMap[question]![index]++;
}
}
});
}
// Convert the frequency map to a single JSON object
Map<String, dynamic> frequencyObject = {};
frequencyMap.forEach((question, frequencies) {
frequencyObject[question] = frequencies;
});
// Encode the frequency object to JSON string
String jsonString = jsonEncode(frequencyObject);
// Return the JSON string
return jsonString;
/// MODIFY CODE ONLY ABOVE THIS LINE
}
My custom function appears to be working correctly. When used with a Text Widget, it returns the expected JSON object.
Now when I try to use JSON Path in the bar chart values e.g. $.measure or $.measure[0:] the array seems to be empty and I receive the error message as shown above.
Is my approach correct or are there alternatives?
I appreciate any help!