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) {
end = json['end'];
if (json['events_after'] != null) {
eventsAfter = <MatrixEvent>[];
json['events_after'].forEach((v) {
eventsAfter.add(MatrixEvent.fromJson(v));
});
eventsAfter = (json['events_after'] as List)
.map((v) => MatrixEvent.fromJson(v))
.toList();
}
event = json['event'] != null ? MatrixEvent.fromJson(json['event']) : null;
if (json['events_before'] != null) {
eventsBefore = <MatrixEvent>[];
json['events_before'].forEach((v) {
eventsBefore.add(MatrixEvent.fromJson(v));
});
eventsBefore = (json['events_before'] as List)
.map((v) => MatrixEvent.fromJson(v))
.toList();
}
start = json['start'];
if (json['state'] != null) {
state = <MatrixEvent>[];
json['state'].forEach((v) {
state.add(MatrixEvent.fromJson(v));
});
state =
(json['state'] as List).map((v) => MatrixEvent.fromJson(v)).toList();
}
}

View File

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

View File

@ -88,13 +88,11 @@ class MatrixException implements Exception {
/// doesn't need additional authentication, then this is null.
List<AuthenticationFlow> get authenticationFlows {
if (!raw.containsKey('flows') || !(raw['flows'] is List)) return null;
var flows = <AuthenticationFlow>[];
for (Map<String, dynamic> flow in raw['flows']) {
if (flow['stages'] is List) {
flows.add(AuthenticationFlow(List<String>.from(flow['stages'])));
}
}
return flows;
return (raw['flows'] as List)
.map((flow) => flow['stages'])
.whereType<List>()
.map((stages) => AuthenticationFlow(List<String>.from(stages)))
.toList();
}
/// 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) {
nextToken = json['next_token'];
notifications = <Notification>[];
json['notifications'].forEach((v) {
notifications.add(Notification.fromJson(v));
});
notifications = (json['notifications'] as List)
.map((v) => Notification.fromJson(v))
.toList();
}
Map<String, dynamic> toJson() {

View File

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

View File

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

View File

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

View File

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

View File

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