GetX
: Flutter ์ํ๊ด๋ฆฌ, ๋ผ์ฐํ
, ์์กด์ฑ ์ฃผ์
๋ผ์ด๋ธ๋ฌ๋ฆฌ
GetX๋ Flutter์์ ์ํ๊ด๋ฆฌ, ๋ผ์ฐํ
, ์์กด์ฑ ์ฃผ์
(DI) ์ ํ ๋ฒ์ ํด๊ฒฐํ ์ ์๋ ๊ฐ๋ ฅํ๊ณ ๊ฐ๋ฒผ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค.
GetX์ ์ฃผ์ ํน์ง
๊ฐ๋จํ ์ํ๊ด๋ฆฌ โ obs(Observable) ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์ํ ์๋ ์
๋ฐ์ดํธ
๋น ๋ฅธ ์ฑ๋ฅ โ ์ต์ํ์ ๋ฆฌ์์ค๋ฅผ ์ฌ์ฉํ์ฌ ์ต์ ํ๋จ
๋ผ์ฐํ
๊ด๋ฆฌ โ Get.to(), Get.off()๋ก ๊ฐํธํ๊ฒ ํ๋ฉด ์ด๋
์์กด์ฑ ์ฃผ์
(DI) โ Get.put(), Get.find()๋ก ๊ฐ์ฒด๋ฅผ ์ฝ๊ฒ ์ฃผ์
๋ณ๋์ context ๋ถํ์ โ BuildContext ์์ด UI ์
๋ฐ์ดํธ ๊ฐ๋ฅ
๋ค์ํ ํ๋ซํผ ์ง์ โ ๋ชจ๋ฐ์ผ, ์น, ๋ฐ์คํฌํฑ๊น์ง ์ง์
GetX ์ค์น
flutter pub add get
Shell
๋ณต์ฌ
๋๋
dependencies:
get: latest_version
YAML
๋ณต์ฌ
GetX ์ํ๊ด๋ฆฌ ์ฌ์ฉ๋ฒ
๊ฐ๋จํ ์ํ๊ด๋ฆฌ (obs ์ฌ์ฉ)
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class CounterController extends GetxController {
var count = 0.obs; // Observable ๋ณ์
void increment() => count++;
}
void main() {
runApp(GetMaterialApp(home: CounterScreen())); // GetMaterialApp ์ฌ์ฉ
}
class CounterScreen extends StatelessWidget {
final CounterController controller = Get.put(CounterController()); // ์์กด์ฑ ์ฃผ์
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('GetX Counter')),
body: Center(
child: Obx(() => Text('Count: ${controller.count}', style: TextStyle(fontSize: 24))),
),
floatingActionButton: FloatingActionButton(
onPressed: controller.increment,
child: Icon(Icons.add),
),
);
}
}
Dart
๋ณต์ฌ
obs๋ฅผ ์ฌ์ฉํ์ฌ ์ํ๋ฅผ ๊ฐ์งํ๊ณ , Obx ์์ ฏ์ ์ฌ์ฉํ์ฌ UI ์๋ ์
๋ฐ์ดํธ!
Get.put()์ผ๋ก Controller๋ฅผ ์ ์ญ์์ ๊ด๋ฆฌ
GetBuilder ์ฌ์ฉ (์ฑ๋ฅ ์ต์ ํ ๊ฐ๋ฅ)
class CounterController extends GetxController {
int count = 0;
void increment() {
count++;
update(); // UI ์
๋ฐ์ดํธ
}
}
class CounterScreen extends StatelessWidget {
Widget build(BuildContext context) {
return GetBuilder<CounterController>(
init: CounterController(),
builder: (controller) => Scaffold(
body: Center(child: Text('Count: ${controller.count}')),
floatingActionButton: FloatingActionButton(
onPressed: controller.increment,
child: Icon(Icons.add),
),
),
);
}
}
Dart
๋ณต์ฌ
GetBuilder๋ ํ์ํ ์์ ฏ๋ง ๋ฆฌ๋ ๋๋ง ํ๋ฏ๋ก ์ฑ๋ฅ ์ต์ ํ ๊ฐ๋ฅ!
Obx์ ๋น๊ตํ์ ๋, ๋ ์ ์ ๋ฆฌ์์ค๋ฅผ ์ฌ์ฉํ๋ฏ๋ก ์ต์ ํ๊ฐ ํ์ํ ๋ ์ฌ์ฉ
GetX ๋ผ์ฐํ
(ํ์ด์ง ์ด๋)
GetX ๋ผ์ฐํ
์ค์
void main() {
runApp(GetMaterialApp(
initialRoute: '/',
getPages: [
GetPage(name: '/', page: () => HomeScreen()),
GetPage(name: '/second', page: () => SecondScreen()),
],
));
}
Dart
๋ณต์ฌ
ํ์ด์ง ์ด๋ (Get.to(), Get.off())
class HomeScreen extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: ElevatedButton(
onPressed: () => Get.to(SecondScreen()), // ๋ค์ ํ์ด์ง๋ก ์ด๋
child: Text('Go to Second Page'),
)),
);
}
}
class SecondScreen extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: ElevatedButton(
onPressed: () => Get.off(HomeScreen()), // ๋ค๋ก๊ฐ๊ธฐ ์์ด ์ด๋
child: Text('Back to Home'),
)),
);
}
}
Dart
๋ณต์ฌ
Get.to(NextScreen()) โ ์๋ก์ด ํ์ด์ง๋ก ์ด๋
Get.off(NextScreen()) โ ํ์ฌ ํ์ด์ง๋ฅผ ์ ๊ฑฐํ๊ณ ์ด๋
Get.offAll(NextScreen()) โ ๋ชจ๋ ์ด์ ํ์ด์ง ์ ๊ฑฐ ํ ์ด๋
GetX ์์กด์ฑ ์ฃผ์
(DI)
์ฆ์ ์ฃผ์
(Get.put())
final CounterController controller = Get.put(CounterController());
Dart
๋ณต์ฌ
โข
์ฑ์ด ์คํ๋๋ฉด ๊ฐ์ฒด๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ์ฆ์ ์์ฑ๋จ
์ง์ฐ ์ฃผ์
(Get.lazyPut())
Get.lazyPut<CounterController>(() => CounterController());
Dart
๋ณต์ฌ
โข
ํ์ํ ๋๋ง ๊ฐ์ฒด ์์ฑ (๋ฉ๋ชจ๋ฆฌ ์ ์ฝ)
์๊ตฌ์ ์ธ ๊ฐ์ฒด (Get.putAsync())
Get.putAsync<CounterController>(() async => CounterController());
Dart
๋ณต์ฌ
โข
๋น๋๊ธฐ ์ด๊ธฐํ๊ฐ ํ์ํ ๊ฒฝ์ฐ ์ฌ์ฉ
GetX vs Bloc vs Provider vs Riverpod ๋น๊ต
๊ธฐ๋ฅ | GetX | Bloc | Provider | Riverpod |
์ํ ๊ด๋ฆฌ | ๊ฐํธํจ (obs, GetBuilder) | ๊ตฌ์กฐ์ (Cubit, Bloc) | ๊ธฐ๋ณธ (ChangeNotifier) | ๊ฐ๋ ฅํจ (StateNotifier, Notifier) |
๋น๋๊ธฐ ์ํ ๊ด๋ฆฌ | RxDart ์์ด ๊ฐ๋ฅ (FutureBuilder ์์ด๋ ๊ฐ๋ฅ) | BlocObserver, Cubit ๋ฑ ์ง์ | ๊ธฐ๋ณธ์ ์ผ๋ก ์ง์ X (FutureBuilder ์ฌ์ฉํด์ผ ํจ) | FutureProvider, AsyncNotifier ์ ๊ณต |
์์กด์ฑ ์ฃผ์
(DI) | Get.put(), Get.find() | RepositoryProvider, MultiBlocProvider | DI ๊ธฐ๋ณธ ์ง์ ์์ (Provider ์ง์ ์ฃผ์
) | Provider ๊ธฐ๋ฐ DI ์ฌ์ฉ (ref.read(), ref.watch()) |
๋ผ์ฐํ
๊ด๋ฆฌ | Get.to(), Get.off() | ์์ | ์์ | ์์ |
์ ์ญ ์ํ ๊ด๋ฆฌ | Get.put()์ผ๋ก ์ฝ๊ฒ ๊ฐ๋ฅ | ๊ฐ๋ฅ (BlocProvider) | ๊ฐ๋ฅ (MultiProvider) | ProviderScope ์ฌ์ฉ |
๋ฐ์ํ ํ๋ก๊ทธ๋๋ฐ ์ง์ | ๊ฐ๋ฅ (RxDart ์์ด๋ ๋ฐ์ํ ๊ฐ๋ฅ) | ๊ฐ๋ ฅํ ์ด๋ฒคํธ ๊ธฐ๋ฐ | ์ ํ์ | ๊ฐ๋ฅ (watch(), listen()) |
์ฝ๋ ๋ณต์ก๋ | ์ฌ์ (๋ณด์ผ๋ฌํ๋ ์ดํธ ์ ์) | ๊ฐ์ฅ ๋ณต์กํจ | ๋ณด์ผ๋ฌํ๋ ์ดํธ ๋ง์ | ๋น๊ต์ ๋ณต์กํจ |
์ฑ๋ฅ ์ต์ ํ | ์ฐ์ (GetBuilder ์ฌ์ฉ ๊ฐ๋ฅ) | ์ฐ์ (BlocListener, BlocObserver) | ๋น๊ต์ ๋ฎ์ | ์ฐ์ (watch(), read()) |
๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ | ๋์ ์ฑ๋ฅ (Get.lazyPut() ์ง์) | ์ด๋ฒคํธ ๋จ์๋ก ์ํ ์ ์ง | ํญ์ ์ธ์คํด์ค ์ ์ง | ํ์ํ ๋๋ง ์ด๊ธฐํ (lazy initialization) |
ํ
์คํธ ์ฉ์ด์ฑ | ์๋์ ์ผ๋ก ๋ถ์กฑ | ๊ฐ์ฅ ์ฐ์ | ๋น๊ต์ ์ฌ์ | ๋งค์ฐ ์ฐ์ |
์ฑ ํฌ๊ธฐ ์ํฅ | ๊ฐ์ฅ ๊ฐ๋ฒผ์ | ๊ฐ์ฅ ๋ฌด๊ฑฐ์ | ์๋์ ์ผ๋ก ๋ฌด๊ฑฐ์ | ๊ฐ๋ฒผ์ |
Flutter ๊ณต์ ์ง์ | ๋น๊ณต์ | Flutter ํ ๊ณต์ ์ง์ | ๊ณต์ ํจํค์ง | ๊ณต์ ํจํค์ง |
๋๊ท๋ชจ ์ฑ ์ ํฉ์ฑ | ์์ ์ฑ์ ์ ํฉ | ๋๊ท๋ชจ ์ฑ์ ๊ฐ์ฅ ์ ํฉ | ๊ฐ๋ฅ | ์คยท๋๊ท๋ชจ ์ฑ ๊ฐ๋ฅ |
GetX๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ๊ฒฝ์ฐ
์ํ๊ด๋ฆฌ + ๋ผ์ฐํ
+ ์์กด์ฑ ์ฃผ์
์ ํ ๋ฒ์ ํด๊ฒฐํ๊ณ ์ถ์ ๋
BuildContext ์์ด UI๋ฅผ ์
๋ฐ์ดํธํ๊ณ ์ถ์ ๋
๋น ๋ฅด๊ณ ๊ฐ๋ฒผ์ด ํผํฌ๋จผ์ค ์ต์ ํ๊ฐ ํ์ํ ๊ฒฝ์ฐ
์ด๋ณด์๋ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์๋ ๊ฐ๋จํ ์ํ๊ด๋ฆฌ๋ฅผ ์ํ ๋
๊ฒฐ๋ก : GetX๋ ๊ฐ์ฅ ๊ฐ๋จํ๋ฉด์๋ ๊ฐ๋ ฅํ Flutter ์ํ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค ํ๋! 






๊ธฐ๋ณธ์ ์ผ๋ก ์ง์ X (