I was running Flutter 1.0.0 (first release) and recently upgraded to 1.2.1 and there were a lot of errors and warnings that I had to correct. Mostly specifying annotation types. After correcting all of these I ran my Android app and now the code that maps JSON to a List is not working. First I'll post my original code that worked. The JSON data is from an HTTP request.
api.dart
Future<List<Device>> getDevices() async {
var response = await _httpNetwork.get(_devicesUrl, headers: {"Cookie": _sessionId});
if (response.statusCode < 200 || response.statusCode > 400) {
throw Exception("Error while fetching data");
}
final body = json.decode(response.body);
return body.map<Device>((device) => Device.fromJson(device)).toList();
}
device.dart
class Device {
Device({
this.id,
this.name,
this.uniqueId,
this.status,
this.phone,
this.model,
this.disabled,
});
factory Device.fromJson(Map<String, dynamic> json) => Device(
id: json['id'],
name: json['name'],
uniqueId: json['uniqueId'],
status: json['status'] == 'online' ? true : false,
phone: json['phone'],
model: json['model'],
disabled: json['disabled'],
);
// API data
int id;
String name;
String uniqueId;
bool status;
String phone;
String model;
bool disabled;
Map<String, dynamic> toJson() => <String, dynamic>{
'id': id,
'name': name,
'uniqueId': uniqueId,
'status': status == true ? 'online' : 'offline',
'phone': phone,
'model': model,
'disabled': disabled,
};
}
Now here is where the issues arises with the following change in api.dart.
return json.decode(response.body).map<Device>((Map<String, dynamic> device) => Device.fromJson(device)).toList();
While this syntax is correct according to Android Studio/Flutter/Dart it doesn't seem to work. The app doesn't crash nor do I get errors in the run console, it just hits my onError code in my Observable<bool>.fromFuture() call.
I put print calls in my code to determine the return statement in api.dart was the culprit. Anyone have any insight into this issue?
[{id: 1, name: Pixel, uniqueId: 439610961385665, status: online, phone: 3215551234, model: XL, disabled: false}]