在构造函数颤动中从 API 更新值

Posted

技术标签:

【中文标题】在构造函数颤动中从 API 更新值【英文标题】:Updating Value From API in a constructor flutter 【发布时间】:2021-09-27 17:59:49 【问题描述】:

我想将来自 page-1 的构造函数的值更新到 page-2。我在 API 响应中使用该值,并希望使用新值更新响应值。 这是更好理解的代码。

第 1 页代码:

 Future<void> forgotpass() async 
if (emailController.text.isNotEmpty) 
  var response = await http
      .post(Uri.parse('http://123.123.123/forgotPassword'),
          headers: <String, String>
            "content-type": "application/json; charset=utf-8",
          ,
          body: jsonEncode(
            "email": emailController.text,
          ))
      .then((response) 
    print(response.statusCode);
    print(response.body);
    final parsedJson = json.decode(response.body);
    var token = parsedJson['data']['forgotPasswordToken'];
    var otp = parsedJson['data']['otp'];
    print('OTP: $otp');
    print("Token: $token");
    if (response.body.contains('"statusCode":200')) 
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => OtpVerifyPage(
                    text: token,
                  )));
     else 
      ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(content: Text('Invalid Email')));
    
    return response;
  );
 else 
  ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Please Enter your Email Addrees')));

令牌已发送到新页面 2。

Future<void> otpVerify() async 
if (otp.text.isNotEmpty) 
  int newOtp = int.parse(otp.text);
  var response = await http
      .post(Uri.parse('http://123.123.123/verifyOtp'),
          headers: <String, String>
            "content-type": "application/json; charset=utf-8"
          ,
          body: jsonEncode(
            "forgotPasswordToken": widget.text, //wanted to update value here from when hitting resendCode() function.
            "otp": newOtp,
          ))
      .then((response) 
    print(response.statusCode);
    print(response.body);
    if (response.body.contains('"statusCode":200')) 
      final parsedJson = json.decode(response.body);
      var resetToken = parsedJson['data']['resetPasswordToken'];
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => SetNewPassPage(
                    text: resetToken,
                  )));
     else if (response.statusCode == 202) 
      ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Server issue. Please Try Again Later')));
     else 
      ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(content: Text('Invalid OTP')));
    
    return response;
  );
 else 
  ScaffoldMessenger.of(context)
      .showSnackBar(SnackBar(content: Text('Please Enter your OTP')));

我也在使用重新发送 OTP 功能:

Future<void> resendCode() async 
// ignore: unused_local_variable
var response = await http
    .post(Uri.parse('http://123.123.123/resendCode'),
        headers: <String, String>
          "content-type": "application/json; charset=utf-8"
        ,
        body: jsonEncode(
          "forgotPasswordToken": widget.text, //Getting new Token but can't update it in otpVerify() function.
        ))
    .then((response) 
  print(response.statusCode);
  print(response.body);
  final parsedJson = json.decode(response.body);
  var token = parsedJson['data']['forgotPasswordToken'];
  var otp = parsedJson['data']['otp'];
  print('OTP: $otp');
  print("Token: $token");
  if (response.body.contains('"statusCode":200')) 
    ScaffoldMessenger.of(context)
        .showSnackBar(SnackBar(content: Text('OTP Send to your Email ID')));
   else 
    ScaffoldMessenger.of(context)
        .showSnackBar(SnackBar(content: Text('Try again later')));
  
  return response;
);

现在我想更新 otpVerify() 主体中的“forgotPasswordToken”。 它在 resendCode() 函数中更新,但不在 otpVerify() 中。因此,如果有人知道如何解决此问题,请提供帮助。

这里是 UI 快照以便更好地理解:Page-1 & Page-2

【问题讨论】:

【参考方案1】:

您可以简单地将其放入 resendCode 函数中,这将使用新值刷新您的页面。

Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => OtpVerifyPage(text: token)));

完整代码以便更好地理解:

Future<void> resendCode() async 
// ignore: unused_local_variable
var response = await http
    .post(Uri.parse('http://123.123.123/resendCode'),
        headers: <String, String>
          "content-type": "application/json; charset=utf-8"
        ,
        body: jsonEncode(
          "forgotPasswordToken": widget.text,
        ))
    .then((response) 
  print(response.statusCode);
  print(response.body);
  final parsedJson = json.decode(response.body);
  var token = parsedJson['data']['forgotPasswordToken'];
  var otp = parsedJson['data']['otp'];
  print('OTP: $otp');
  print("Token: $token");
  if (response.body.contains('"statusCode":200')) 
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => OtpVerifyPage(text: token))); // Added here to refresh the page with new value of token.
    ScaffoldMessenger.of(context)
        .showSnackBar(SnackBar(content: Text('OTP Send to your Email ID')));
   else 
    ScaffoldMessenger.of(context)
        .showSnackBar(SnackBar(content: Text('Try again later')));
  
  return response;
);

【讨论】:

以上是关于在构造函数颤动中从 API 更新值的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中从类外部访问私有构造函数

在java中从另一个调用一个构造函数[重复]

如何在颤动中从 api url 加载图像?

如何在 Java 中从另一个构造函数调用一个构造函数?

如何在主方法中从类中调用构造函数

如何在 C++ 中从 dll(dll 中的构造函数)创建一些类?