refactor: use map

This commit is contained in:
Lukas Lihotzki 2021-05-05 12:05:26 +02:00
parent 19500307be
commit 83ef6484eb
9 changed files with 29 additions and 53 deletions

View File

@ -34,24 +34,20 @@ class EventContext {
EventContext.fromJson(Map<String, dynamic> json) { EventContext.fromJson(Map<String, dynamic> json) {
end = json['end']; end = json['end'];
if (json['events_after'] != null) { if (json['events_after'] != null) {
eventsAfter = <MatrixEvent>[]; eventsAfter = (json['events_after'] as List)
json['events_after'].forEach((v) { .map((v) => MatrixEvent.fromJson(v))
eventsAfter.add(MatrixEvent.fromJson(v)); .toList();
});
} }
event = json['event'] != null ? MatrixEvent.fromJson(json['event']) : null; event = json['event'] != null ? MatrixEvent.fromJson(json['event']) : null;
if (json['events_before'] != null) { if (json['events_before'] != null) {
eventsBefore = <MatrixEvent>[]; eventsBefore = (json['events_before'] as List)
json['events_before'].forEach((v) { .map((v) => MatrixEvent.fromJson(v))
eventsBefore.add(MatrixEvent.fromJson(v)); .toList();
});
} }
start = json['start']; start = json['start'];
if (json['state'] != null) { if (json['state'] != null) {
state = <MatrixEvent>[]; state =
json['state'].forEach((v) { (json['state'] as List).map((v) => MatrixEvent.fromJson(v)).toList();
state.add(MatrixEvent.fromJson(v));
});
} }
} }

View File

@ -26,10 +26,7 @@ class LoginTypes {
LoginTypes.fromJson(Map<String, dynamic> json) { LoginTypes.fromJson(Map<String, dynamic> json) {
if (json['flows'] != null) { if (json['flows'] != null) {
flows = <Flows>[]; flows = (json['flows'] as List).map((v) => Flows.fromJson(v)).toList();
json['flows'].forEach((v) {
flows.add(Flows.fromJson(v));
});
} }
} }

View File

@ -88,13 +88,11 @@ class MatrixException implements Exception {
/// doesn't need additional authentication, then this is null. /// doesn't need additional authentication, then this is null.
List<AuthenticationFlow> get authenticationFlows { List<AuthenticationFlow> get authenticationFlows {
if (!raw.containsKey('flows') || !(raw['flows'] is List)) return null; if (!raw.containsKey('flows') || !(raw['flows'] is List)) return null;
var flows = <AuthenticationFlow>[]; return (raw['flows'] as List)
for (Map<String, dynamic> flow in raw['flows']) { .map((flow) => flow['stages'])
if (flow['stages'] is List) { .whereType<List>()
flows.add(AuthenticationFlow(List<String>.from(flow['stages']))); .map((stages) => AuthenticationFlow(List<String>.from(stages)))
} .toList();
}
return flows;
} }
/// This section contains any information that the client will need to know in order to use a given type /// This section contains any information that the client will need to know in order to use a given type

View File

@ -29,10 +29,9 @@ class NotificationsQueryResponse {
NotificationsQueryResponse.fromJson(Map<String, dynamic> json) { NotificationsQueryResponse.fromJson(Map<String, dynamic> json) {
nextToken = json['next_token']; nextToken = json['next_token'];
notifications = <Notification>[]; notifications = (json['notifications'] as List)
json['notifications'].forEach((v) { .map((v) => Notification.fromJson(v))
notifications.add(Notification.fromJson(v)); .toList();
});
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {

View File

@ -28,10 +28,7 @@ class PublicRoomsResponse {
int totalRoomCountEstimate; int totalRoomCountEstimate;
PublicRoomsResponse.fromJson(Map<String, dynamic> json) { PublicRoomsResponse.fromJson(Map<String, dynamic> json) {
chunk = <PublicRoom>[]; chunk = (json['chunk'] as List).map((v) => PublicRoom.fromJson(v)).toList();
json['chunk'].forEach((v) {
chunk.add(PublicRoom.fromJson(v));
});
nextBatch = json['next_batch']; nextBatch = json['next_batch'];
prevBatch = json['prev_batch']; prevBatch = json['prev_batch'];
totalRoomCountEstimate = json['total_room_count_estimate']; totalRoomCountEstimate = json['total_room_count_estimate'];

View File

@ -34,10 +34,9 @@ class SupportedProtocol {
icon = json['icon']; icon = json['icon'];
fieldTypes = (json['field_types'] as Map) fieldTypes = (json['field_types'] as Map)
.map((k, v) => MapEntry(k, ProtocolFieldType.fromJson(v))); .map((k, v) => MapEntry(k, ProtocolFieldType.fromJson(v)));
instances = <ProtocolInstance>[]; instances = (json['instances'] as List)
json['instances'].forEach((v) { .map((v) => ProtocolInstance.fromJson(v))
instances.add(ProtocolInstance.fromJson(v)); .toList();
});
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {

View File

@ -28,11 +28,8 @@ class UserSearchResult {
bool limited; bool limited;
UserSearchResult.fromJson(Map<String, dynamic> json) { UserSearchResult.fromJson(Map<String, dynamic> json) {
results = <Profile>[]; results =
json['results'].forEach((v) { (json['results'] as List).map((v) => Profile.fromJson(v)).toList();
results.add(Profile.fromJson(v));
});
limited = json['limited']; limited = json['limited'];
} }

View File

@ -48,10 +48,8 @@ class DeviceInfo {
DeviceInfo.fromJson(Map<String, dynamic> json) { DeviceInfo.fromJson(Map<String, dynamic> json) {
if (json['sessions'] != null) { if (json['sessions'] != null) {
sessions = <Sessions>[]; sessions =
json['sessions'].forEach((v) { (json['sessions'] as List).map((v) => Sessions.fromJson(v)).toList();
sessions.add(Sessions.fromJson(v));
});
} }
} }
@ -69,10 +67,9 @@ class Sessions {
Sessions.fromJson(Map<String, dynamic> json) { Sessions.fromJson(Map<String, dynamic> json) {
if (json['connections'] != null) { if (json['connections'] != null) {
connections = <Connections>[]; connections = (json['connections'] as List)
json['connections'].forEach((v) { .map((v) => Connections.fromJson(v))
connections.add(Connections.fromJson(v)); .toList();
});
} }
} }

View File

@ -27,11 +27,7 @@ extension MapCopyExtension on Map<String, dynamic> {
return value.copy(); return value.copy();
} }
if (value is List) { if (value is List) {
final ret = []; return value.map(_copyValue).toList();
for (final val in value) {
ret.add(_copyValue(val));
}
return ret;
} }
return value; return value;
} }