0. GetX๋?
Provider์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ํ๊ด๋ฆฌ๋ฅผ ์ํ ํจํค์ง๋ค. Docs์์๋ GetX๋ ์์ฐ์ฑ, ์ฑ๋ฅ, ์กฐ์งํ์ 3๊ฐ์ง ๊ธฐ๋ณธ ์์น์ ๊ฐ์ง๊ณ ์๋ค๊ณ ๋งํ๋ค.
- ์์ฐ์ฑ
- ์ฝ๊ณ ์น์ํ ๊ตฌ๋ฌธ ์ฌ์ฉ
- ๊ฐ๋ฐ ์๊ฐ์ ์๋ ์ ์์ผ๋ฉฐ, ์ ํ๋ฆฌ์ผ์ด์ ์ ์ต๋ ์ฑ๋ฅ์ผ๋ก ์ ๊ณต ๊ฐ๋ฅ
- ์ฑ๋ฅ
- ์ฑ๋ฅ๊ณผ ์ต์ํ์ ๋ฆฌ์์ค ์๋น์ ์ค์ ์ ๋
- Streams๋ ChangeNotifier์ ์ฌ์ฉํ์ง ์์
- ์กฐ์งํ
- ํ๋ฉด, ํ๋ ์ ํ ์ด์ ๋ก์ง, ๋น์ง๋์ค ๋ก์ง, ์ข ์์ฑ ์ฃผ์ ๋ฐ ๋ค๋น๊ฒ์ด์ ์์ ๋ถ๋ฆฌ ๊ฐ๋ฅ
- ๋ผ์ฐํธ๊ฐ ์ ํ์ ํ๋ ๋ฐ์ context๊ฐ ํ์ํ์ง ์์
- ์์ ฏ ํธ๋ฆฌ์ ๋ ๋ฆฝ์
- inheritedWidget์ ํตํด ์ปจํธ๋กค๋ฌ/๋ธ๋ก์ ์ ๊ทผํ๋ ๋ฐ์ context ๋ฏธํ์
- ๋ค์ค Provider๋ฅผ ํตํด ์์ ฏ ํธ๋ฆฌ์์ ์ปจํธ๋กค๋ฌ, ๋ชจ๋ธ, ๋ธ๋ก์ผ๋ก ์ฃผ์
ํ ํ์ ์์
- ์์ฒด ์ข ์์ฑ ์ฃผ์ ๊ธฐ๋ฅ ์ฌ์ฉ
Flutter ํ์์ ๊ถ์ฅํ๋ BLoC์์๋ ๋น์ง๋์ค ๋ก์ง๊ณผ ์๊ฐ ๊ฐ์ฒด๋ฅผ ๋ถ๋ฆฌํ๊ณ ์์ง๋ง, GetX๋ ๋ชจ๋ ๋ก์ง์ ๋ถ๋ฆฌํ๋ค.
์ข ์์ฑ ์ฃผ์ ๊ณผ ๋ผ์ฐํธ ๋ํ ๋ถ๋ฆฌ๋๋ฉฐ, ๋ฐ์ดํฐ ๊ณ์ธต์ด ๋ชจ๋ ๋ถ๋ฆฌ๋์ด ๊ฐ ๊ธฐ๋ฅ์ ๋์ฑ ์ฝ๊ฒ ์ฐพ์ ์ ์๊ฒ ๋๋ค.
์ํ ๊ด๋ฆฌ๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ, ์ํ ๊ด๋ฆฌ๋ ์ปดํ์ผ์ด ๋๋ฉฐ GetX์์ ์ ๊ณตํ๋ ๋ผ์ฐํธ๋ง ์ฌ์ฉํ ๊ฒฝ์ฐ์๋ ์ํ ๊ด๋ฆฌ๊ฐ ๋ฐ๋ก ์ปดํ์ผ ๋์ง ์๋๋ค.
1. GetX์ ์ํ๊ด๋ฆฌ
GetX์ ์ํ ๊ด๋ฆฌ ๋ฐฉ๋ฒ์ ๋ ๊ฐ์ง๋ก ๋๋์ด์ง๋ค. ๋จ์ ์ํ ๊ด๋ฆฌ์ ๋ฐ์ ์ํ ๊ด๋ฆฌ๋ก ๋๋๋๋ฐ, ๋ฐ์ํ ์ํ ๊ด๋ฆฌ๋ ๊ฐ๊ฒฐํ ๋ฌธ๋ฒ์ผ๋ก ๊ฐ์ ๋ณ๊ฒฝ์ ๊ฐ์งํ์ฌ ์ฌ๋ ๋๋ง์ ํ๋ฉฐ, ๋จ์ ์ํ ๊ด๋ฆฌ๋ ๋ฐ์ดํฐ์ ๋ณ๊ฒฝ๋๋ ๋ฐ์ดํฐ๊ฐ ๊ฐ์ ์ง์ ๋ํ ํ์ธ์ ํ์ง ์๋๋ค.
๋จ์ ์ํ ๊ด๋ฆฌ
import 'package:get/get.dart';
class SimpleController extends GetxController {
int counter = 0;
void increase() {
counter++;
update();
}
}
๋จ์ ์ํ ๊ด๋ฆฌ๋ฅผ ์ํ controller๋ฅผ ์์ฑํด ์ฃผ์๋ค. ์ด controller๋ counter๋ผ๋ ๋ณ์์ ์ด ๋ณ์์ ๊ฐ์ 1์ฉ ์ฆ๊ฐ์์ผ ์ฃผ๋ increase ํจ์๋ฅผ ์์ ํ๊ณ ์๋ค.
ํด๋น ํจ์ ๋ด์๋ update๋ผ๋ ํจ์๊ฐ ์๋๋ฐ, ์ด ํจ์๋ controller๊ฐ ๋ฐ๋ผ๋ณด๋ ๋ชจ๋ ์ฝ๋์ ์ ๋ฐ์ดํธ๋ฅผ ์๋ฆฌ๋ ์ญํ ์ ํ๊ฒ ๋๋ค.
class MyHomePage extends StatelessWidget {
MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
Get.put(SimpleController()); // controller ๋ฑ๋ก
return Scaffold(
appBar: AppBar(
title: const Text("๋จ์ ์ํ๊ด๋ฆฌ"),
),
body: Center(
child: GetBuilder<SimpleController>( // ์ค์๊ฐ ๋ ๋๋ง
builder: (controller) {
return ElevatedButton(
child: Text(
'ํ์ฌ ์ซ์: ${controller.counter}',
),
onPressed: () {
controller.increase();
},
);
},
),
),
);
}
}
์ด์ ๋ง๋ค์ด๋ธ controller๋ฅผ ์ฌ์ฉํด ๋ณด์.
controller๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด Get.put()์ผ๋ก controller๋ฅผ ๋ฑ๋กํด์ฃผ๋ฉด GetBuilder() ์๋์ ์๋ ๋ชจ๋ ์์ ฏ์ controller์์ ๋ณ๊ฒฝ๋๋ ๋ฐ์ดํฐ๋ฅผ ์ค์๊ฐ์ผ๋ก ๊ฐ์งํ๋ฉฐ ๋ฐ์ํ ์ ์๊ฒ ๋๋ค.
๋ง์ฝ GetBuilder๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ง ์๋ค๋ฉด Get.find<[Controller]>().[๋ณ์ or ํจ์]๋ฅผ ์ฌ์ฉํ๋ฉด ์ํ ๊ด๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ค.
๋ฐ์ ์ํ ๊ด๋ฆฌ
import 'package:get/get.dart';
class ReactiveController extends GetxController {
RxInt counter = 0.obs;
void increase() {
counter++;
}
}
๋ฐ์ ์ํ ๊ด๋ฆฌ๋ฅผ ์ํ Controller๋ฅผ ์์ฑํ๋ค.
์์ ๋์ผํ๊ฒ counter๋ผ๋ ๋ณ์์ ํด๋น ๋ณ์์ ๊ฐ์ 1์ฉ ์ฆ๊ฐ์ํค๋ ํจ์๋ฅผ ๋ง๋ค์ด ์ค๋ค.
๋์ ๋จ์ ์ํ ๊ด๋ฆฌ์ ๋ณ์๋ฅผ ์ ์ธํ๋ ๋ฐฉ์๊ณผ ์ ๋ฐ์ดํธ ํจ์์ ํํ๊ฐ ๋ค๋ฅด๋ค.
๋ณ์ ์ ์ธ์ ๋ณ์ ํ์ ์ RxInt, RxString ๋ฑ๊ณผ ๊ฐ์ด Rx๋ฅผ ๋ถ์ด๊ฒ ๋๋ฉฐ, ๋ณ์์ ๊ฐ ๋ค์๋ .obs๋ฅผ ๋ถ์ฌ์ฃผ๊ฒ ๋๋ค.
์ด๋ ๊ฒ ์ ์ธํ ๋ณ์๋ update ํจ์๊ฐ ์์ด๋ ๋ฌด๊ดํ๋ค.
class MyHomePage extends StatelessWidget {
MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
Get.put(ReactiveController()); // ๋ฐ์ํ ์ํ ๊ด๋ฆฌ controller ๋ฑ๋ก
return Scaffold(
appBar: AppBar(
title: const Text("๋จ์ / ๋ฐ์ํ ์ํ๊ด๋ฆฌ"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GetX<ReactiveController>( // ๋ฐ์ํ ์ํ๊ด๋ฆฌ - 1
builder: (controller) {
return ElevatedButton(
child: Text(
'๋ฐ์ํ 1 / ํ์ฌ ์ซ์: ${controller.counter.value}', // .value ๋ก ์ ๊ทผ
),
onPressed: () {
controller.increase();
// Get.find<ReactiveController>().increase();
},
);
},
),
Obx( // ๋ฐ์ํ ์ํ๊ด๋ฆฌ - 2
() {
return ElevatedButton(
child: Text(
'๋ฐ์ํ 2 / ํ์ฌ ์ซ์: ${Get.find<ReactiveController>().counter.value}', // .value ๋ก ์ ๊ทผ
),
onPressed: () {
Get.find<ReactiveController>().increase();
},
);
},
),
],
),
),
);
}
}
controller๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ๋์ผํ๊ฒ Get.put()์ผ๋ก controller๋ฅผ ๋ฑ๋กํด์ค๋ค.
์ฌ๊ธฐ์ ๋ฐ์ดํฐ ์ค์๊ฐ ๋ฐ์ํ๋ ๋ฐฉ๋ฒ์ผ๋ก ๋ ๊ฐ์ง๊ฐ ์๋๋ฐ GetX() - GetX() ๋ฐฉ๋ฒ๊ณผ Obx() - Obx() ๋ฐฉ๋ฒ์ด ์๋ค.
- GetX() - GetX()
์๋ ๋ชจ๋ ์์ ฏ์ด.controller์์ ๋ณ๊ฒฝ๋๋ ๋ฐ์ดํฐ๋ฅผ ์ค์๊ฐ์ผ๋ก ๋ฐ์ํ ์ ์๋ ์ํ๊ฐ ๋๋ฉฐ, controller.counter.value์ ๊ฐ์ด ๋ค์ .value๋ฅผ ๋ถ์ฌ ์ฌ์ฉํด์ผ ํ๋ค.
๋จ์ ์ํ ๊ด๋ฆฌ์ ๋์ผํ๊ฒ GetX()๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ง ์๋ค๋ฉด Get.find<[Controller]>().[๋ณ์ or ํจ์]๋ฅผ ์ฌ์ฉํ๋ฉด ์ํ ๊ด๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ค.
- Obx() - Obx()
๋์ผํ๊ฒ ์๋ ๋ชจ๋ ์์ ฏ์ด controller์์ ๋ณ๊ฒฝ๋๋ ๋ฐ์ดํฐ๋ฅผ ์ค์๊ฐ์ผ๋ก ๋ฐ์ํ ์ ์๋ ์ํ๊ฐ ๋๋ค.
์ฌ์ฉ ๋ฐฉ์์ ๊ฑฐ์ ๋์ผํ์ง๋ง GetX()์ ๋ฌ๋ฆฌ controller์ ์ด๋ฆ์ ์ง์ ํ ์ ์์ด Get.find() ๋ฐฉ์์ด ํ์์ ์ด๋ค.
๋ํ, ๋ฐ์ ์ํ ๊ด๋ฆฌ์๋ worker๋ผ๋ ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ด ์๋ค. controller ์์์ onInit() ํจ์๋ฅผ override ํ ํ, ๊ทธ ๋ด๋ถ์์ ์ฌ์ฉํ๊ฒ ๋๋ค
- Ever: ๋งค๋ฒ ๋ณ๊ฒฝ ์ ์คํ
- Once: ์ฒ์ ๋ณ๊ฒฝ๋์์ ๋๋ง ์คํ
- Interval: ๊ณ์ ๋ณ๊ฒฝ์ด ์๋ ๋์ ํน์ ์ง์ ์๊ฐ ์ธํฐ๋ฒ ํ ์คํ
- Debounce: ์ธํฐ๋ฒ์ด ๋๋ ํ, ํน์ ์ง์ ์๊ฐ ์ดํ ํ ๋ฒ๋ง ์คํ
์ฌ์ฉ๋ฒ์ ์๋์ ๊ฐ๋ค.
import 'package:get/get.dart';
class ReactiveController extends GetxController {
static ReactiveController get to => Get.find();
RxInt counter = 0.obs;
@override
void onInit() {
once(counter, (_) {
print('once : $_์ด ์ฒ์์ผ๋ก ๋ณ๊ฒฝ๋์์ต๋๋ค.');
});
ever(counter, (_) {
print('ever : $_์ด ๋ณ๊ฒฝ๋์์ต๋๋ค.');
});
debounce(
counter,
(_) {
print('debounce : $_๊ฐ ๋ง์ง๋ง์ผ๋ก ๋ณ๊ฒฝ๋ ์ดํ, 1์ด๊ฐ ๋ณ๊ฒฝ์ด ์์ต๋๋ค.');
},
time: Duration(seconds: 1),
);
interval(
counter,
(_) {
print('interval $_๊ฐ ๋ณ๊ฒฝ๋๋ ์ค์
๋๋ค.(1์ด๋ง๋ค ํธ์ถ)');
},
time: Duration(seconds: 1),
);
super.onInit();
}
void increase() {
counter++;
}
}
Get.find()๋ฅผ ์ข ๋ ๊ฐ๋จํ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์๋ค.
- Getter ์ฌ์ฉ
class SimpleController extends GetxController {
static SimpleController get to => Get.find();
...
}
- GetView ์ฌ์ฉ
Get.find()๋ฅผ ์ฌ์ฉํ๋ ํด๋์ค์ StatelessWidget ๋์ GetView๋ฅผ ์์๋ฐ๋๋ค.
class SimpleState extends GetView<SimpleController>{}
controller.increase();
์ฐธ๊ณ
[Flutter] GetX โก - ์ํ ๊ด๋ฆฌ
get | Flutter Package Open screens/snackbars/dialogs without context, manage states and inject dependencies easily with GetX. pub.dev ์ค์ pubspec.yaml dependencies: get: ^4.6.5 main.dart MaterialApp ์ GetMaterialApp ์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. GetX ๋ฅผ
dalgoodori.tistory.com
Flutter - GetX๋ฅผ ์ด์ฉํ ์ํ๊ด๋ฆฌ
GetX๋ฅผ ์ด์ฉํ Flutter ์ํ๊ด๋ฆฌ์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค.
danawalab.github.io
GetX ์ ๋ํด ์์๋ณด์
GetX ์ฌ์ฉ๋ฒ์ ์ ๋๋ก ๋ฝ์ค๋ณด์.
velog.io
getx/README.ko-kr.md at master · jonataslaw/getx
Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get. - jonataslaw/getx
github.com
get | Flutter package
Open screens/snackbars/dialogs without context, manage states and inject dependencies easily with GetX.
pub.dev
Flutter ์ธ๊ธฐ ์ํคํ ์ฒ ๋ผ์ด๋ธ๋ฌ๋ฆฌ 3์ข ๋น๊ต ๋ถ์ - GetX vs BLoC vs Provider
์๋ ํ์ธ์. LINE+ ABC Studio์์ ์ฑ์ ๊ฐ๋ฐํ๊ณ ์๋ ์ค๊ธฐ์์ ๋๋ค. ์ต๊ทผ Flutter๋ก ์งํํ๋ ์๋ก์ด ์ฑ ๊ฐ๋ฐ ์ ๋ฌด๋ฅผ ๋งก์์ ์ด๋ค ์ํคํ ์ฒ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ์ง ์ ์ ํ๋ ์์ ์ ์งํํ์ต๋๋ค.
engineering.linecorp.com
'Study > Flutter' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Flutter] Flutter์ ์ํ๊ด๋ฆฌ - Riverpod (0) | 2025.01.31 |
---|---|
[Flutter] Flutter์ ์ํ๊ด๋ฆฌ - Provider (0) | 2025.01.17 |