Skip to content

Instantly share code, notes, and snippets.

@PlugFox
Last active May 16, 2022 16:17
Show Gist options
  • Save PlugFox/6a6d73a7822001af7f8558df89dbc60d to your computer and use it in GitHub Desktop.
Save PlugFox/6a6d73a7822001af7f8558df89dbc60d to your computer and use it in GitHub Desktop.
Dart data class example
/*
* Dart data class example
* https://gist.github.com/PlugFox/6a6d73a7822001af7f8558df89dbc60d
* https://dartpad.dev/6a6d73a7822001af7f8558df89dbc60d
* Matiunin Mikhail <plugfox@gmail.com>, 16 May 2022
*/
import 'package:meta/meta.dart';
void main() =>
print(RideNotificationState.received(id: 1, title: 'Title').toJson());
/// {@template ride_notification_state}
/// RideNotificationState
/// {@endtemplate}
@immutable
// ignore: prefer_mixin
abstract class RideNotificationState with Comparable<RideNotificationState> {
/// {@macro ride_notification_state}
RideNotificationState({
required this.id,
required this.title,
this.description = '',
DateTime? updated,
DateTime? created,
}) : updated = updated ?? DateTime.now(),
created = created ?? updated ?? DateTime.now();
/// {@macro received_ride_notification_state}
factory RideNotificationState.received({
required int id,
required String title,
String description,
DateTime? updated,
DateTime? created,
}) = ReceivedRideNotificationState;
/// {@macro removed_ride_notification_state}
factory RideNotificationState.removed({
required int id,
required String title,
String description,
DateTime? updated,
DateTime? created,
}) = ReceivedRideNotificationState;
/// Generate Class from Map<String, Object?>
factory RideNotificationState.fromJson(Map<String, Object?> json) {
switch (json['type']) {
case 'received':
return ReceivedRideNotificationState.fromJson(json);
case 'removed':
return RemovedRideNotificationState.fromJson(json);
default:
throw StateError('Unknown type: ${json['type'] ?? 'null'}');
}
}
/// The unique identifier for this object.
final int id;
/// The title of this object.
final String title;
/// A short description of this object.
final String description;
/// The date this object was last updated.
final DateTime updated;
/// The date this object was created.
final DateTime created;
/// This is removed state
bool get isRemoved => maybeMap<bool>(
orElse: () => false,
removed: (r) => true,
);
/// Get received state or null
ReceivedRideNotificationState? get receivedOrNull =>
maybeMap<ReceivedRideNotificationState?>(
orElse: () => null,
received: (state) => state,
);
/// Generate Map<String, Object?> from class
Map<String, Object?> toJson() => <String, Object?>{
'id': id,
'title': title,
'description': description,
'updated': updated.toUtc().toIso8601String(),
'created': created.toUtc().toIso8601String(),
};
/// Create a copy of this object with the given values.
RideNotificationState copyWith({
int? newId,
String? newTitle,
String? newDescription,
DateTime? newUpdated,
DateTime? newCreated,
});
/// Map object
T map<T>({
required T Function(ReceivedRideNotificationState state) received,
required T Function(RemovedRideNotificationState state) removed,
});
/// Map object
T maybeMap<T>({
required T Function() orElse,
T Function(ReceivedRideNotificationState state)? received,
T Function(RemovedRideNotificationState state)? removed,
}) =>
map<T>(
received: received ?? (_) => orElse(),
removed: removed ?? (_) => orElse(),
);
@override
int compareTo(RideNotificationState other) => id.compareTo(other.id);
@override
int get hashCode => id;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is RideNotificationState &&
runtimeType == other.runtimeType &&
id == other.id;
@override
String toString() => 'RideNotificationState#$id';
} // RideNotificationState
/// {@template received_ride_notification_state}
/// ReceivedRideNotificationState
/// {@endtemplate}
@immutable
class ReceivedRideNotificationState extends RideNotificationState {
/// {@macro received_ride_notification_state}
ReceivedRideNotificationState({
required super.id,
required super.title,
super.description = '',
super.updated,
super.created,
});
/// Generate Class from Map<String, Object?>
factory ReceivedRideNotificationState.fromJson(Map<String, Object?> json) =>
ReceivedRideNotificationState(
id: (json['id'] as int?)!,
title: (json['title'] as String?)!,
description: (json['description'] as String?)!,
updated: DateTime.parse((json['updated'] as String?)!).toLocal(),
created: DateTime.parse((json['created'] as String?)!).toLocal(),
);
@override
Map<String, Object?> toJson() => <String, Object?>{
'type': 'received_ride_notification_state',
...super.toJson(),
};
@override
ReceivedRideNotificationState copyWith({
int? newId,
String? newTitle,
String? newDescription,
DateTime? newUpdated,
DateTime? newCreated,
}) =>
ReceivedRideNotificationState(
id: newId ?? id,
title: newTitle ?? title,
description: newDescription ?? description,
updated: newUpdated ?? DateTime.now(),
created: newCreated ?? created,
);
@override
T map<T>({
required T Function(ReceivedRideNotificationState state) received,
required T Function(RemovedRideNotificationState state) removed,
}) =>
received(this);
@override
String toString() => 'ReceivedRideNotificationState#$id';
} // ReceivedRideNotificationState
/// {@template removed_ride_notification_state}
/// RemovedRideNotificationState
/// {@endtemplate}
@immutable
class RemovedRideNotificationState extends RideNotificationState {
/// {@macro removed_ride_notification_state}
RemovedRideNotificationState({
required super.id,
required super.title,
super.description = '',
super.updated,
super.created,
});
/// Generate Class from Map<String, Object?>
factory RemovedRideNotificationState.fromJson(Map<String, Object?> json) =>
RemovedRideNotificationState(
id: (json['id'] as int?)!,
title: (json['title'] as String?)!,
description: (json['description'] as String?)!,
updated: DateTime.parse((json['updated'] as String?)!).toLocal(),
created: DateTime.parse((json['created'] as String?)!).toLocal(),
);
@override
Map<String, Object?> toJson() => <String, Object?>{
'type': 'removed_ride_notification_state',
...super.toJson(),
};
@override
RemovedRideNotificationState copyWith({
int? newId,
String? newTitle,
String? newDescription,
DateTime? newUpdated,
DateTime? newCreated,
}) =>
RemovedRideNotificationState(
id: newId ?? id,
title: newTitle ?? title,
description: newDescription ?? description,
updated: newUpdated ?? DateTime.now(),
created: newCreated ?? created,
);
@override
T map<T>({
required T Function(ReceivedRideNotificationState state) received,
required T Function(RemovedRideNotificationState state) removed,
}) =>
removed(this);
@override
String toString() => 'RemovedRideNotificationState#$id';
} // RemovedRideNotificationState
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment