Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: prefer {List,Map}.of over {List,Map}.from #4333

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/content/docs/faqs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bloc.
:::caution
`Equatable` properties should always be copied rather than modified. If an
`Equatable` class contains a `List` or `Map` as properties, be sure to use
`List.from` or `Map.from` respectively to ensure that equality is evaluated
`List.of` or `Map.of` respectively to ensure that equality is evaluated
based on the values of the properties rather than the reference.
:::

Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/ko/faqs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import BlocExternalForEachSnippet from '~/components/faqs/BlocExternalForEachSni
<StateNotUpdatingBad3Snippet />

:::caution
`Equatable` 프로퍼티는 항상 수정하지 말고 복사해야 합니다. `Equatable` 클래스에 `List` 또는 `Map`이 프로퍼티로 있는 경우, 참조가 아닌 프로퍼티 값을 기준으로 동등성이 평가되도록 `List.from` 또는 `Map.from`을 각각 사용해야 합니다.
`Equatable` 프로퍼티는 항상 수정하지 말고 복사해야 합니다. `Equatable` 클래스에 `List` 또는 `Map`이 프로퍼티로 있는 경우, 참조가 아닌 프로퍼티 값을 기준으로 동등성이 평가되도록 `List.of` 또는 `Map.of`을 각각 사용해야 합니다.
:::

## 언제 Equatable를 사용해야 하나요
Expand Down
18 changes: 7 additions & 11 deletions packages/hydrated_bloc/test/cubits/list_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:meta/meta.dart';
class ListCubit extends HydratedCubit<List<String>> {
ListCubit() : super(const <String>[]);

void addItem(String item) => emit(List.from(state)..add(item));
void addItem(String item) => emit(List.of(state)..add(item));

@override
Map<String, dynamic> toJson(List<String> state) {
Expand All @@ -22,16 +22,14 @@ class ListCubitMap<T extends ToJsonMap<E>, E> extends HydratedCubit<List<T>> {
final T Function(Map<String, dynamic> json) _fromJson;
final bool explicit;

void addItem(T item) => emit(List.from(state)..add(item));
void addItem(T item) => emit(List.of(state)..add(item));

@override
Map<String, dynamic> toJson(List<T> state) {
final map = <String, dynamic>{
'state': explicit
? List<Map<String, E>>.from(
state.map<dynamic>(
(x) => x.toJson(),
),
? List<Map<String, E>>.of(
state.map((x) => x.toJson()),
)
: state,
};
Expand All @@ -53,17 +51,15 @@ class ListCubitList<T extends ToJsonList<E>, E> extends HydratedCubit<List<T>> {
final T Function(List<dynamic> json) _fromJson;
final bool explicit;

void addItem(T item) => emit(List.from(state)..add(item));
void addItem(T item) => emit(List.of(state)..add(item));
void reset() => emit(<T>[]);

@override
Map<String, dynamic> toJson(List<T> state) {
final map = <String, dynamic>{
'state': explicit
? List<List<E>>.from(
state.map<dynamic>(
(x) => x.toJson(),
),
? List<List<E>>.of(
state.map((x) => x.toJson()),
)
: state,
};
Expand Down