Skip to main content

Bloc Event and State

  • flutter_bloc: 7.0.0
  • equatable: 2.0.0

Event#

Event ๋ช…์€ Bloc ๋ช… + (์ˆ˜์‹ํ•  ๋ช…์‚ฌ) ์š”์ฒญ์‚ฌํ•ญ(๊ณผ๊ฑฐ ๋™์‚ฌ)์œผ๋กœ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•˜์ง€๋งŒ ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ์ž‘์„ฑํ•˜๋“  ์ƒ๊ด€ ์—†์Šต๋‹ˆ๋‹ค.

Event๋Š” enum ๋˜๋Š” class๋กœ ์ •์˜ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Event๊ฐ€ ๋‹จ์ˆœํ•œ ๊ฒฝ์šฐ enum์œผ๋กœ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ๋‹จ์ˆœํ•œ ์ปจํŠธ๋กค๋Ÿฌ์™€ ๊ด€๋ จ๋œ Event๋ฅผ ๋งŒ๋“ ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

part of 'controller_bloc.dart';
enum ControllerEvent { fanOnRequested, fanOffRequested }

ํ•˜์ง€๋งŒ ์„ ํ’๊ธฐ์˜ ํ’์†์ด๋‚˜ ๋ฐฉํ–ฅ ๋“ฑ์„ ์กฐ์ ˆํ•˜๊ธฐ ์œ„ํ•ด Event์™€ ํ•จ๊ป˜ ์„ค์ • ๊ฐ’์„ ์•Œ๋ ค์ค˜์•ผํ•œ๋‹ค๋ฉด, class๋กœ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

part of 'controller_bloc.dart';
abstract class ControllerEvent {
const ControllerEvent();
}
class ControllerFanRequested extends ControllerEvent {
final bool autoRotation;
final double strength;
const ControllerFanRequested.on(
{this.autoRotation = false, this.strength = 0.5});
const ControllerFanRequested.off()
: autoRotation = false,
strength = 0;
const ControllerFanRequested.startAutoRotation({this.strength = -1})
: autoRotation = true;
const ControllerFanRequested.stopAutoRotation({this.strength = -1})
: autoRotation = false;
}

State#

State ๋ช…์€ Bloc ๋ช… + ์ทจํ•œ ํ–‰๋™(๋™์‚ฌ) + ์ƒํƒœ๋กœ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•˜์ง€๋งŒ ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ์ž‘์„ฑํ•˜๋“  ์ƒ๊ด€ ์—†์Šต๋‹ˆ๋‹ค.

Event์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ enum ๋˜๋Š” class๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ถˆํ•„์š”ํ•œ ๋ Œ๋”๋ง์„ ๋ง‰๊ธฐ ์œ„ํ•ด State์˜ ๊ฒฝ์šฐ ์ค‘๋ณต๋œ ๊ฒƒ์€ ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค.

info

์ค‘๋ณต ๊ฒ€์‚ฌ๋Š” ๋‹จ์ˆœํ•˜๊ฒŒ previousState == nextState๊ฐ€ ์ฐธ์ธ์ง€ ์•„๋‹Œ์ง€๋งŒ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.

part of 'controller_bloc.dart';
enum ControllerState {
fanOnRequestSuccess,
fanOnRequestFailure,
fanOffRequestSuccess,
fanOffRequestFailure,
}

State๊ฐ€ ๊ฐ™๋”๋ผ๋„ presentation layer๋ฅผ ๊ฐฑ์‹ ํ•˜๊ธธ ์›ํ•˜๋Š” ๊ฒฝ์šฐ enum์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ class๋กœ ๋งŒ๋“ค๊ณ , ํ•ญ์ƒ ์ƒˆ๋กœ ์ƒ์„ฑํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ class๋ผ๋„ ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋Š” ๋‹ค๋ฅด๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

part of 'controller_bloc.dart';
enum ControllerRequestStatus { success, failure }
abstract class ControllerState {
const ControllerState();
}
class ControllerInitial extends ControllerState {}
class ControllerFanRequestState extends ControllerState {
final ControllerRequestStatus status;
final bool autoRotation;
final double strength;
const ControllerFanRequestState.failure()
: status = ControllerRequestStatus.failure,
autoRotation = false,
strength = 0;
const ControllerFanRequestState.success(this.autoRotation, this.strength)
: status = ControllerRequestStatus.success;
}

๋งŽ์€ ์ƒํ™ฉ์—์„œ ์ค‘๋ณต๋œ State ์ฒ˜๋ฆฌ๋ฅผ ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ class๋ฅผ ์‚ฌ์šฉํ•˜๋”๋ผ๋„, ์ค‘๋ณต๋œ ๊ฐ’์ธ์ง€ ํ™•์ธํ•  ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. Equatable์„ ์ƒ์†ํ•˜์—ฌ Event๋ฅผ ๋งŒ๋“ค๊ณ , props๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜์—ฌ Event์˜ ํƒ€์ž…๊ณผ porps์— ์„ ์–ธ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์ค‘๋ณต์œผ๋กœ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

part of 'controller_bloc.dart';
enum ControllerRequestStatus { success, failure }
abstract class ControllerState extends Equatable {
const ControllerState();
}
class ControllerInitial extends ControllerState {
@override
List<Object?> get props => [];
}
class ControllerFanRequestState extends ControllerState {
final ControllerRequestStatus status;
final bool autoRotation;
final double strength;
const ControllerFanRequestState.failure()
: status = ControllerRequestStatus.failure,
autoRotation = false,
strength = 0;
const ControllerFanRequestState.success(this.autoRotation, this.strength)
: status = ControllerRequestStatus.success;
@override
List<Object?> get props => [status, autoRotation, strength];
}
info

props ๋ฆฌ์ŠคํŠธ์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋“ค๋„ ๋‹จ์ˆœํ•˜๊ฒŒ previousState.props[0] == nextState.props[0] ์ด๋Ÿฐ์‹์œผ๋กœ ํŒ๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋น„๊ต ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜๊ฐ€ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Last updated on