创建UserService继承自GetxService
class UserService extends GetxService {
User _user;
User get user=>_user;
bool get isLogin => _user != null;
}
注入依赖
void main() {
Get.put(UserService());
runApp(MyApp());
}
使用依赖
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
//Get.find()获取依赖并使用里面的方法
var UserService = Get.find<UserService>();
if (UserService.isLogin) {
}
}
}