ever(rxData, (value) {
//함수
})
유저의 로그인 정보가 바뀔 수 있다.
-> 유저가 로그인 하여 데이터가 바뀌면 페이지 이동시키는 것을 연결한다.
-> ever worker
Rxn<User> userInfo = Rxn<User>();
...
ever(Rxn<User>, (value) {
if(Rxn<User> != null){
Get.to(() => 메인페이지());
return ;
} else {
Get.to(() => 로그인페이지());
return ;
}
})
import 'package:get/get.dart';
import 'login_page.dart';
import 'user.dart';
import 'user_page.dart';
class AppController extends GetxController {
Rxn<User> user = Rxn();
void onInit() {
//controller 가 실행되자마자 사용되는 함수
super.onInit();
ever(user, (value) {
if (user != null) {
Get.to(() => UserPage());
return;
} else {
Get.to(() => LoginPage());
return;
}
});
}
}
class User {
String id;
String nickname;
User({
required this.id,
required this.nickname,
});
}
import 'package:day15_login/login_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
Widget build(BuildContext context) {
return GetMaterialApp(
home: LoginPage(),
);
}
}
import 'package:day15_login/app_controller.dart';
import 'package:day15_login/user.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class LoginPage extends StatelessWidget {
const LoginPage({super.key});
Widget build(BuildContext context) {
var controller = Get.put(AppController());
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('LoginPage'),
TextButton(
onPressed: () {
controller.user(User(id: 'dd', nickname: 'ddd'));
},
child: Text('Login'))
],
)));
;
}
}
import 'package:day15_login/app_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'user.dart';
class UserPage extends StatelessWidget {
const UserPage({super.key});
Widget build(BuildContext context) {
var controller = Get.find<AppController>();
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('UserPage'),
TextButton(
onPressed: () {
controller.user.value = null;
},
child: Text('Logout'))
],
)));
}
}