Skip to content

Instantly share code, notes, and snippets.

@hawkkiller
Created June 23, 2022 08:50
Show Gist options
  • Save hawkkiller/c82a016c9a714e41e3810f29d81557ad to your computer and use it in GitHub Desktop.
Save hawkkiller/c82a016c9a714e41e3810f29d81557ad to your computer and use it in GitHub Desktop.
Freezed vs Plain Class
@freezed
class UserCardsState with _$UserCardsState {
const UserCardsState._();
bool get inProgress => maybeMap(
fetching: (_) => true,
orElse: () => false,
);
bool get isFetched => maybeMap(
fetched: (_) => true,
orElse: () => false,
);
factory UserCardsState.initial({
@Default(<UserCard>[]) final List<UserCard> cards,
}) = _InitialUserCardsState;
factory UserCardsState.fetching({
@Default(<UserCard>[]) final List<UserCard> cards,
}) = _FetchingUserCardsState;
factory UserCardsState.fetched({
required final List<UserCard> cards,
}) = _FetchedUserCardsState;
factory UserCardsState.error({
@Default(<UserCard>[]) final List<UserCard> cards,
@Default('No Message') final String message,
}) = _ErrorUserCardsState;
}
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:m10/features/transfer_linked_card/model/transfer_linked_card_model.dart';
/// interface
@immutable
abstract class UserCardsState {
const UserCardsState({
required this.userCards,
});
final List<UserCard> userCards;
@override
int get hashCode;
@override
bool operator ==(Object other);
}
class InitialUserCardsState extends UserCardsState {
const InitialUserCardsState([
final List<UserCard>? cards,
]) : super(
userCards: cards ?? const <UserCard>[],
);
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is InitialUserCardsState &&
const DeepCollectionEquality().equals(other.userCards, userCards));
}
@override
int get hashCode =>
Object.hash(runtimeType, const DeepCollectionEquality().hash(userCards));
}
class FetchingUserCardsState extends UserCardsState {
const FetchingUserCardsState([
final List<UserCard>? cards,
]) : super(
userCards: cards ?? const <UserCard>[],
);
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is FetchingUserCardsState &&
const DeepCollectionEquality().equals(other.userCards, userCards));
}
@override
int get hashCode =>
Object.hash(runtimeType, const DeepCollectionEquality().hash(userCards));
}
class FetchedUserCardsState extends UserCardsState {
const FetchedUserCardsState({
required final List<UserCard> cards,
}) : super(userCards: cards);
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is FetchedUserCardsState &&
const DeepCollectionEquality().equals(other.userCards, userCards));
}
@override
int get hashCode =>
Object.hash(runtimeType, const DeepCollectionEquality().hash(userCards));
}
class ErrorUserCardsState extends UserCardsState {
const ErrorUserCardsState({
final List<UserCard>? cards,
this.message = 'Empty Message',
}) : super(userCards: cards ?? const <UserCard>[]);
final String message;
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is ErrorUserCardsState &&
const DeepCollectionEquality().equals(other.userCards, userCards) &&
const DeepCollectionEquality().equals(other.message, message));
}
@override
int get hashCode => Object.hash(
runtimeType,
const DeepCollectionEquality().hash(userCards),
const DeepCollectionEquality().hash(message),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment