diff --git a/lib/src/model/event_context.dart b/lib/src/model/event_context.dart index 2bdc026d..aeee869e 100644 --- a/lib/src/model/event_context.dart +++ b/lib/src/model/event_context.dart @@ -34,24 +34,20 @@ class EventContext { EventContext.fromJson(Map json) { end = json['end']; if (json['events_after'] != null) { - eventsAfter = []; - 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 = []; - 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 = []; - json['state'].forEach((v) { - state.add(MatrixEvent.fromJson(v)); - }); + state = + (json['state'] as List).map((v) => MatrixEvent.fromJson(v)).toList(); } } diff --git a/lib/src/model/login_types.dart b/lib/src/model/login_types.dart index f95098ad..de8dca7f 100644 --- a/lib/src/model/login_types.dart +++ b/lib/src/model/login_types.dart @@ -26,10 +26,7 @@ class LoginTypes { LoginTypes.fromJson(Map json) { if (json['flows'] != null) { - flows = []; - json['flows'].forEach((v) { - flows.add(Flows.fromJson(v)); - }); + flows = (json['flows'] as List).map((v) => Flows.fromJson(v)).toList(); } } diff --git a/lib/src/model/matrix_exception.dart b/lib/src/model/matrix_exception.dart index 00af277e..1e1b3bf4 100644 --- a/lib/src/model/matrix_exception.dart +++ b/lib/src/model/matrix_exception.dart @@ -88,13 +88,11 @@ class MatrixException implements Exception { /// doesn't need additional authentication, then this is null. List get authenticationFlows { if (!raw.containsKey('flows') || !(raw['flows'] is List)) return null; - var flows = []; - for (Map flow in raw['flows']) { - if (flow['stages'] is List) { - flows.add(AuthenticationFlow(List.from(flow['stages']))); - } - } - return flows; + return (raw['flows'] as List) + .map((flow) => flow['stages']) + .whereType() + .map((stages) => AuthenticationFlow(List.from(stages))) + .toList(); } /// This section contains any information that the client will need to know in order to use a given type diff --git a/lib/src/model/notifications_query_response.dart b/lib/src/model/notifications_query_response.dart index 01b2777c..aaeadbda 100644 --- a/lib/src/model/notifications_query_response.dart +++ b/lib/src/model/notifications_query_response.dart @@ -29,10 +29,9 @@ class NotificationsQueryResponse { NotificationsQueryResponse.fromJson(Map json) { nextToken = json['next_token']; - notifications = []; - json['notifications'].forEach((v) { - notifications.add(Notification.fromJson(v)); - }); + notifications = (json['notifications'] as List) + .map((v) => Notification.fromJson(v)) + .toList(); } Map toJson() { diff --git a/lib/src/model/public_rooms_response.dart b/lib/src/model/public_rooms_response.dart index 56671440..34ae0bf7 100644 --- a/lib/src/model/public_rooms_response.dart +++ b/lib/src/model/public_rooms_response.dart @@ -28,10 +28,7 @@ class PublicRoomsResponse { int totalRoomCountEstimate; PublicRoomsResponse.fromJson(Map json) { - chunk = []; - 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']; diff --git a/lib/src/model/supported_protocol.dart b/lib/src/model/supported_protocol.dart index 0f0c1b30..a81c6bd8 100644 --- a/lib/src/model/supported_protocol.dart +++ b/lib/src/model/supported_protocol.dart @@ -34,10 +34,9 @@ class SupportedProtocol { icon = json['icon']; fieldTypes = (json['field_types'] as Map) .map((k, v) => MapEntry(k, ProtocolFieldType.fromJson(v))); - instances = []; - json['instances'].forEach((v) { - instances.add(ProtocolInstance.fromJson(v)); - }); + instances = (json['instances'] as List) + .map((v) => ProtocolInstance.fromJson(v)) + .toList(); } Map toJson() { diff --git a/lib/src/model/user_search_result.dart b/lib/src/model/user_search_result.dart index 837a08ec..27c84d7c 100644 --- a/lib/src/model/user_search_result.dart +++ b/lib/src/model/user_search_result.dart @@ -28,11 +28,8 @@ class UserSearchResult { bool limited; UserSearchResult.fromJson(Map json) { - results = []; - json['results'].forEach((v) { - results.add(Profile.fromJson(v)); - }); - + results = + (json['results'] as List).map((v) => Profile.fromJson(v)).toList(); limited = json['limited']; } diff --git a/lib/src/model/who_is_info.dart b/lib/src/model/who_is_info.dart index be144736..5504a24d 100644 --- a/lib/src/model/who_is_info.dart +++ b/lib/src/model/who_is_info.dart @@ -48,10 +48,8 @@ class DeviceInfo { DeviceInfo.fromJson(Map json) { if (json['sessions'] != null) { - 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 json) { if (json['connections'] != null) { - connections = []; - json['connections'].forEach((v) { - connections.add(Connections.fromJson(v)); - }); + connections = (json['connections'] as List) + .map((v) => Connections.fromJson(v)) + .toList(); } } diff --git a/lib/src/utils/map_copy_extension.dart b/lib/src/utils/map_copy_extension.dart index 9e3018b7..2cf95989 100644 --- a/lib/src/utils/map_copy_extension.dart +++ b/lib/src/utils/map_copy_extension.dart @@ -27,11 +27,7 @@ extension MapCopyExtension on Map { 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; }