如何在flutter的不同包中的另一个函数中使用来自未来函数的变量

时间:2021-07-16 19:32:00

标签: flutter api http dart

我需要从这个未来函数“登录”中调用变量“profile”或“doctor_id”到 Flutter 中的另一个屏幕。 不久我想将数据从服务器打印到我的应用程序,该应用程序来自 api 作为响应,并在另一个屏幕中重用它。

 Future Signin(String email, password) async {
    var URL = "https://phpclinic.000webhostapp.com/Doctor/SignIn.php";
    Map mappeddata = {
      'email': email,
      'password': password,
    };
    print("Your data is: ${mappeddata}");
    var response = await http.post(URL, body: mappeddata);
    print("${response.body}");
    var data = json.decode(response.body);
    if (response != null) {
      if (data == "dont have an account") {
        showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                backgroundColor: Colors.blue[300],
                title: Text(
                  AppLocalization.of(context)
                      .translate("error"),
                  style: TextStyle(
                      color: Colors.blue, fontSize: 35),
                ),
                content: Text('dont have an account',
                  style: TextStyle(fontSize: 25),
                ),
                actions: <Widget>[
                  RaisedButton(
                      child: Text(
                        AppLocalization.of(context)
                            .translate('end'),
                        style: TextStyle(
                            color: Colors.white, fontSize: 20),
                      ),
                      color: Colors.blue[400],
                      onPressed: () {
                        Navigator.of(context).pop();
                      })
                ],
              );
            });
      }
      else {
        if (data == "false") {
          showDialog(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(
                  backgroundColor: Colors.blue[300],
                  title: Text(
                    AppLocalization.of(context)
                        .translate("error"),
                    style: TextStyle(
                        color: Colors.blue, fontSize: 35),
                  ),
                  content: Text('incorrect password or mail',
                    style: TextStyle(fontSize: 25),
                  ),
                  actions: <Widget>[
                    RaisedButton(
                        child: Text(
                          AppLocalization.of(context)
                              .translate('end'),
                          style: TextStyle(
                              color: Colors.white, fontSize: 20),
                        ),
                        color: Colors.blue[400],
                        onPressed: () {
                          Navigator.of(context).pop();
                        })
                  ],
                );
              });
        }
        else {
          profile =json.decode(response.body);
          doctor_id=profile[0]['doctor_id'];
          print("the list is: ${profile}");
          print("the doctor_id is: ${doctor_id}");
          Fluttertoast.showToast(
              msg: "Welcome", toastLength: Toast.LENGTH_LONG);
          Navigator.push(
              context, MaterialPageRoute(builder: (context) => HomeScreen()));

        }
      }
    }
  }

所以我可以以“Coming.fromJson(profile)”的形式使用它

    Future<Comeing> getData() async {
    var URL = "https://phpclinic.000webhostapp.com/Doctor/SignIn.php";
    var postResponse = await http.get(URL);
    var data = json.decode(postResponse.body);
    if (data) {
      Comeing.fromJson(data);
      print("the data is: ${data}");
    }
  }

所以我可以在我的代码中使用响应的内容并检索变量。 提前致谢!

0 个答案:

没有答案