I am trying to output data from database using parsedFromJson and getDataFromDB future method but I am getting error and is not resolving. I am an experienced developer but new to flutter. I never spent this amount of time to resolve anything in other tech stacks before
class House_Effects {
final String? ID;
final String? Title;
final DateTime? Date;
House_Effects({required this.ID, required this.Title, required this.Date });
factory House_Effects .fromJson(Map<String, dynamic> json) {
return House_Effects (
ID: json['ID'].toString(),
Title: json['Title'].toString(),
Date: json['Date']
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['ID'] = this.ID.toString();
data['Title'] = this.Title;
data['Date'] = this.Date;
return data;
}
}
List<House_Effects > parsedFromJson(String responseBody) {
List<dynamic> jsonData = jsonDecode(responseBody);
List<Map<String, dynamic>>? endParsed = [];
for (int idx = 0; idx < jsonData.length; idx++)
{
final parsed = (jsonDecode(responseBody)) as Map<String,dynamic>;
endParsed.add(parsed);
}
return endParsed.map<House_Effects >((json) =>
House_Effects.fromJson(json)).toList();
}
Future<List<House_Effects>> getDataFromDB() async {
List<House_Effects> listEffects = [];
const String bUrl = 'http://17627.9.9.1:4008';
final response = await http.get(Uri.parse('$bUrl/show'));
if (response.statusCode == 200) {
listEffects = await parsedFromJson(response.body);
} else {
throw Exception('Failed to getdata');
};
return listEffects;
}
}
—JSArray: type List is not a subtype of type Map<String, dynamic>
Flutter Dart
THE ERROR is mainly from final parsed = (jsonDecode(responseBody)) as Map<String,dynamic>;
user27403201 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.