【问题标题】:How to get data of user after authorization in flutter如何在flutter中授权后获取用户数据
【发布时间】:2020-01-16 13:49:57
【问题描述】:

我在颤抖中挣扎。我的逻辑部分在 Authorization bloc 中,UI 是 Authorization screen,只是调用这个方法。我通过 Firebase 的电话号码获得了授权。

一切正常,验证后我得到用户响应、访问和刷新令牌。之后,我需要根据此用户响应推送到另一个屏幕。如果我已经授权,那么我的个人资料数据响应将被填写,我只需将其推送到主屏幕。

但是当我是新用户时,用户响应数据将为空,因此我需要推送到个人资料屏幕,在那里我将填写我的名字、姓氏和电子邮件。那么如何在 UI 屏幕中查看我的数据呢?

这是我的代码

SIGN IN 授权块中的方法

Future<bool> signIn(String smsCode) async {
      dynamic userInfo;
      String firstName;
      String lastName;
      String email;

      bool result = true;
      AuthCredential authCredential = PhoneAuthProvider.getCredential(verificationId: this._verificationId, smsCode: smsCode);

      final FirebaseUser user = await FirebaseAuth.instance.signInWithCredential(authCredential);
      final FirebaseUser currentUser = await FirebaseAuth.instance.currentUser();
      assert(user.uid == currentUser.uid);
      final String idToken = await currentUser.getIdToken(refresh: true);
      _api.validateIdToken(idToken).then((response) {
         final model = new CustomerModel.fromJson(response);
         String password = response["password"];
         _api.getAccessToken(_phoneNumber, password).then((dynamic response2) {
          _accessToken = response2["access_token"];
          _refreshToken = response2["refresh_token"];
        });
      });

      if(firstName ==null && lastName == null && email == null){
        return false;
      }
      return result;
  }

在我的授权屏幕

中调用该方法
void _doLogin() {

    FocusScope.of(context).requestFocus(FocusNode());

    if (pinCode.length < 6) {
      _scaffoldKey.currentState.showSnackBar(
        SnackBar(
          duration: Duration(seconds: 3),
          content: Text('Пожалуйста введите код'),
        ),
      );
    } else {       
          Fluttertoast.showToast(
            msg: "Авторизация...$pinCode",
            toastLength: Toast.LENGTH_LONG,
            gravity: ToastGravity.BOTTOM,
            timeInSecForIos: 3,
            backgroundColor: Colors.grey,
            textColor: Colors.white,
            fontSize: 13.0
         );
          bloc.signIn(pinCode).then((hasProfile){
            print(hasProfile);
            if(hasProfile){
              Navigator.push(context,MaterialPageRoute(builder: (context) => ProfileData()));
            }else {
              Navigator.push(context, MaterialPageRoute(builder: (context) => HomeScreen()));
            }
          });
    }
  }

如你所见,我这样称呼它bloc.signIn(pinCode).then((hasProfile){...,但之后的条件不起作用。所以我想也许我需要更新状态或使用流或接收器。我只是 bloc 的新手,这就是我迷路的原因。请指导我。在此先感谢:)

【问题讨论】:

    标签: flutter authorization bloc


    【解决方案1】:

    存储数据或数据管理有两种方法。一种方法是从小部件向下传递到小部件。 Pass down to widget

    另一种方法是在一个地方存储和检索所有数据。 Single source of truth

    这是两种方式。对于第一种方法,可以将 statefulWidgets 与 setState 一起使用。如果您有大型或可扩展的应用程序,则不建议使用这种方式。

    使用第二种方法,有多种存储数据的方式,例如 redux、BLoC/Rx、Provider 或 MobX。 Flutter 推荐使用 provider 方法。文档是一个很棒的地方,它以非常清晰的方式准确地解释了如何做到这一点:docs

    如果您有任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 2020-11-12
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      • 2021-03-17
      • 2020-10-25
      相关资源
      最近更新 更多