๋ธ๋ก๊ทธ ๋ด์ ๊ฒ์๋ฌผ์ PC ๋ฒ์ ์ ์ต์ ํ ๋์ด ์์ต๋๋ค.
์ค๋์ Equatable์ ๋ํด ์์๋ณด๋ ค๊ณ ํฉ๋๋ค. BLoC์ ๋ํ ๊ธ์ ์ฐ๊ธฐ ์ ์ BLoC์์ ์ฌ์ฉํ๋ Equatable์ ๋ํ ์ ๋ฆฌ๊ฐ ์ฐ์ ์ ์ด๋ผ๊ณ ์๊ฐํ์ต๋๋ค. ๋ฐ๋ก ๋ณธ๋ก ์ผ๋ก ๋ค์ด๊ฐ๋ณด์ฃ !
์๋ ์ฝ๋๋ฅผ ๋จผ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
class Person {
final int pid;
final String name;
final int age;
Person(this.pid, this.name, this.age);
}
void main() {
Person p1 = new Person(20221234, "kodo", 25);
Person p2 = new Person(20221234, "kodo", 25);
(p1 == p2) ? print("same") : print("diff");
}
Person ํด๋์ค๋ฅผ ์ ์ํ๊ณ ๋ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ์์ฑํ์ต๋๋ค. ์ฌ์๋ฒํธ, ์ด๋ฆ, ๋์ด๊ฐ ๊ฐ์ ๋ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ ๋ ์ธ์คํด์ค๋ฅผ ๋น๊ตํ์ต๋๋ค. ์ธ์คํด์ค๊ฐ ๊ฐ๋ค๋ฉด same์ ์ถ๋ ฅํ๊ณ ๊ทธ๋ ์ง ์๋ค๋ฉด diff๋ฅผ ์ถ๋ ฅํฉ๋๋ค. ๋ฌด์์ด ์ถ๋ ฅ๋ ๊น์?
์ ๋ต์ diff ์ ๋๋ค. ์ ๊ทธ๋ด๊น์? ๊ฐ์ ๋ฉค๋ฒ ๋ณ์๋ฅผ ๊ฐ์ง์ง๋ง new ํค์๋๋ฅผ ํตํด ์๋ก์ด ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ธฐ์ ์๋ก ๋ค๋ฅธ ์ฃผ์๋ฅผ ์ฐธ์กฐํ๊ณ ์์ต๋๋ค. ๊ทธ๋ ๋ค๋ฉด ์ด๋ฐ ์ํฉ์์ ์ด๋ป๊ฒํ๋ฉด same์ ์ถ๋ ฅํ ์ ์์๊น์?
์ฐ์ ๊ฐ๋จํ๊ฒ main์์ ๋ฉค๋ฒ ๋ณ์๋ฅผ ๋น๊ตํ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
class Person {
final int pid;
final String name;
final int age;
Person(this.pid, this.name, this.age);
}
void main() {
Person p1 = new Person(20221234, "kodo", 25);
Person p2 = new Person(20221234, "kodo", 25);
(p1.pid == p2.pid) ? print("same") : print("diff");
}
ํ์ง๋ง pid๋ฅผ ์ง์ ์ฐธ์กฐํด์ผ ํ๋ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋ ์ข์ ๋ฐฉ๋ฒ์ ์์๊น์?
== ์ฐ์ฐ์๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ์ฌ ๋ฉค๋ฒ ๋ณ์๊ฐ ๊ฐ์ผ๋ฉด ๊ฐ์ ์ธ์คํด์ค๋ผ๋ ๊ฒ์ ๋ํ๋ด๋ฉด ๋ฉ๋๋ค.
class Person {
final int pid;
final String name;
final int age;
Person(this.pid, this.name, this.age);
@override
bool operator ==(Object other) {
return other is Person && other.pid == this.age;
}
}
void main() {
Person p1 = new Person(20221234, "kodo", 25);
Person p2 = new Person(20221234, "kodo", 25);
(p1.pid == p2.pid) ? print("same") : print("diff");
}
๋๋ฌ์ต๋๋ค! ์ฌ๊ธฐ๊น์ง ์ ๋ฐ๋ผ์ค์
จ๋ค๋ฉด Equatable์ ์๋ฒฝํ ์ดํดํ์ ๊ฒ๋๋ค. ๐๐ (๋ฒ์จ์?)
Equatable
์๋ก ์ด ๊ธธ์์ง๋ง ์ค๋ ์๊ธฐํ Equatable์ ์ธ์คํด์ค์ ๋น๊ต๋ฅผ ์ฝ๊ฒ ํ ์ ์๋๋ก ๋์์ค๋๋ค. ์ฐ์ Equatable์ ์ฝ๋๋ ์ด๋ ๊ฒ ์๊ฒผ์ต๋๋ค.
@immutable
abstract class Equatable {
/// {@macro equatable}
const Equatable();
/// {@template equatable_props}
/// The list of properties that will be used to determine whether
/// two instances are equal.
/// {@endtemplate}
List<Object?> get props;
/// {@template equatable_stringify}
/// If set to `true`, the [toString] method will be overridden to output
/// this instance's [props].
///
/// A global default value for [stringify] can be set using
/// `EquatableConfig.stringify`.
///
/// If this instance's [stringify] is set to null, the value of
/// `EquatableConfig.stringify` will be used instead. This defaults to
/// `false`.
/// {@endtemplate}
// ignore: avoid_returning_null
bool? get stringify => null;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Equatable &&
runtimeType == other.runtimeType &&
equals(props, other.props);
@override
int get hashCode => runtimeType.hashCode ^ mapPropsToHashCode(props);
@override
String toString() {
switch (stringify) {
case true:
return mapPropsToString(runtimeType, props);
case false:
return '$runtimeType';
default:
return EquatableConfig.stringify == true
? mapPropsToString(runtimeType, props)
: '$runtimeType';
}
}
}
props๋ฅผ ๋น๊ตํ์ฌ ๋ ์ธ์คํด์ค๊ฐ ๊ฐ์์ง๋ฅผ ํ๋ณํฉ๋๋ค. ๋ํ props๋ก hashcode๋ฅผ ์์ฑํฉ๋๋ค. hashcode์ ๊ฒฝ์ฐ ๋์ผํ key๋ก ์ค๋ณต ์ ์ฅ๋ ์ ์๋ Map๊ณผ Set์์ ์ฌ์ฉ๋ฉ๋๋ค.
Equatable์ ์์ํ๋ Person ํด๋์ค๋ฅผ ์ ์ํ๊ณ props์ getter๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ๋ฉด ๋์ ๋๋ค!
import 'package:equatable/equatable.dart';
class Person extends Equatable {
final int pid;
final String name;
final int age;
Person(this.pid, this.name, this.age);
@override
List<Object?> get props => [this.pid];
}
void main() {
Person p1 = new Person(20221234, "kodo", 25);
Person p2 = new Person(20221234, "kodo", 25);
(p1.pid == p2.pid) ? print("same") : print("diff");
}
๊ทธ๋ ๋ค๋ฉด ์ BLoC์ ํ์ํ๊ฑฐ์ฃ ?
BLoC์์๋ Event๊ฐ ๋ฐ์ํ๋ฉด State๊ฐ ๋ณ๊ฒฝ๋ ์ ์๋๋ก Mapping์ ํฉ๋๋ค. ์ดํ State๋ฅผ ๊ฐ์งํ๊ณ State์ ๋ง๋ ์์ ์ ์งํํฉ๋๋ค. ์ด๋ ๊ฐ๊ฐ์ Event์ State๊ฐ ์๋ก ๋ค๋ฅธ ์ธ์คํด์ค๋ผ๋ ๊ฒ์ ์ ์ ์๋๋ก Equatable์ ์์๋ฐ์ ๊ตฌํํฉ๋๋ค.
'๐ป ๊ฐ๋ฐ > Flutter' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Flutter] ModalBottomSheet๊ฐ ํค๋ณด๋์ ์ํด ๊ฐ๋ ค์ง๋ ํ์ (0) | 2022.08.09 |
---|---|
[Flutter] BLoC ํจํด์ผ๋ก ์๋ ๋ก๊ทธ์ธ, Splash Screen ๊ตฌํํ๊ธฐ - 1 (0) | 2022.07.26 |
[Flutter / Dart] What is Singleton? (0) | 2022.07.21 |
[Flutter] SingleChildScrollView, ListView, ListView.bulider (1) | 2021.08.18 |
4. Column, Row, Expanded (0) | 2021.08.18 |