首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >颤振-如何在使用GetX重定向之前检查用户角色

颤振-如何在使用GetX重定向之前检查用户角色
EN

Stack Overflow用户
提问于 2022-04-06 07:20:07
回答 1查看 182关注 0票数 0
代码语言:javascript
运行
复制
   void onReady() {
    super.onReady();
    firebaseUser = Rx<User?>(auth.currentUser);
    firebaseUser.bindStream(auth.userChanges());

    //GetX function that listens to user state changes
    ever(firebaseUser, _setInitialScreen);
  }

    //gets called immediately after sign up, giving no chance to check user role
   _setInitialScreen(User? user) async {
    if (user == null) {
      Get.offAll(() => AuthenticationScreen());
    } 

      //Need to check user role here (before going to else)
      else {
          if (userModel.value.role == "fieldOwner") {
            Get.offAll(() => FieldOwnerHome(user: user));
          } else {
            Get.offAll(() => HomeScreen(user: user));
          }
    }
  }

在调用其他任何内容之前,该控件将运行到Get.offAll()。

代码语言:javascript
运行
复制
  void signUp() async {
    showLoading();
    try {
      await auth
          .createUserWithEmailAndPassword(
              email: email.text.trim(), password: password.text.trim())
          .then((result) async {
        String? _userId = result.user?.uid;
        await _addUserToFirestore(_userId);

       //function to get user and check role
        await _initializeUserModel(_userId);

        _clearControllers();
      });
    } catch (e) {
      debugPrint(e.toString());
      Get.snackbar(
        "Sign Up Failed",
        e.toString(),
        snackPosition: SnackPosition.BOTTOM,
      );
      dismissLoading();
    }
  }
EN

回答 1

Stack Overflow用户

发布于 2022-04-06 08:26:42

尝试将代码从onReady()移动到onInit()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71762564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档