앱이 시작될 때:
StartUpViewModel 만들기앱 시작 시 필요한 로직을 담을 ViewModel을 생성합니다.
class StartUpViewModel extends FutureViewModel {
Future<void> futureToRun() async {
final user = await _authService.getCurrentUser();
if (user != null) {
// 홈으로 이동
_navigationService.replaceWithHomeView();
} else {
// 로그인으로 이동
_navigationService.replaceWithLoginView();
}
}
}
FutureViewModel을 상속받으면 futureToRun() 함수가 자동 실행됩니다.NavigationService를 통해 이동을 처리합니다.StartUpView 만들기class StartUpView extends StackedView<StartUpViewModel> {
Widget builder(BuildContext context, StartUpViewModel viewModel, Widget? child) {
return const Scaffold(
body: Center(child: CircularProgressIndicator()),
);
}
StartUpViewModel viewModelBuilder(BuildContext context) => StartUpViewModel();
}
NavigationService를 사용하면 ViewModel 내에서 자유롭게 화면 전환을 할 수 있습니다.
final _navigationService = locator<NavigationService>();
_navigationService.replaceWithHomeView(); // 홈 화면으로 이동
_navigationService.replaceWithLoginView(); // 로그인 화면으로 이동
replaceWith...는 현재 화면을 대체합니다. 뒤로 가기 불가능.
| 단계 | 설명 |
|---|---|
StartUpViewModel | 앱 시작 시 로직 처리 (ex. 로그인 상태 확인) |
StartUpView | 로딩만 보여주고, ViewModel이 라우팅을 담당 |
NavigationService | ViewModel에서 화면 전환 책임 |
이 구조를 사용하면, 앱 시작 시 로직을 UI와 분리하여 깔끔하게 구성할 수 있습니다.