GetX
: Flutter ์ํ๊ด๋ฆฌ, ๋ผ์ฐํ
, ์์กด์ฑ ์ฃผ์
๋ผ์ด๋ธ๋ฌ๋ฆฌ
GetX๋ Flutter์์ ์ํ๊ด๋ฆฌ, ๋ผ์ฐํ
, ์์กด์ฑ ์ฃผ์
(DI) ์ ํ ๋ฒ์ ํด๊ฒฐํ ์ ์๋ ๊ฐ๋ ฅํ๊ณ ๊ฐ๋ฒผ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค.
GetX์ ์ฃผ์ ํน์ง
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
๋ณต์ฌ
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
๋ณต์ฌ
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
๋ณต์ฌ
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 |
์ํ ๊ด๋ฆฌ | ||||
๋น๋๊ธฐ ์ํ ๊ด๋ฆฌ | ||||
์์กด์ฑ ์ฃผ์
(DI) | ||||
๋ผ์ฐํ
๊ด๋ฆฌ | ||||
์ ์ญ ์ํ ๊ด๋ฆฌ | ||||
๋ฐ์ํ ํ๋ก๊ทธ๋๋ฐ ์ง์ | ||||
์ฝ๋ ๋ณต์ก๋ | ||||
์ฑ๋ฅ ์ต์ ํ | ||||
๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ | ||||
ํ
์คํธ ์ฉ์ด์ฑ | ||||
์ฑ ํฌ๊ธฐ ์ํฅ | ||||
Flutter ๊ณต์ ์ง์ | ||||
๋๊ท๋ชจ ์ฑ ์ ํฉ์ฑ |