在Flutter中编写HTTP发布请求的方式

问题描述 投票:0回答:1

我正在编写用于酒店注册的应用程序,在该应用程序中,我将发送如下所示的发帖请求>

checkNetworkConnection只是为了检查id设备上是否存在活动连接,如果为true,那么我正在调用postFormData

方法
Future<dynamic> checkNetworkConnection() async {
    try {
      final result = await InternetAddress.lookup("www.google.com");
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        postFormData().then((val) {
          if (jsonDecode(val.body)[0]["response"] == "false") {
            setState(() {
              isLoading = false;
              //isAbsorbing = false;
            });
            print(val.body);
            print("something went wrong");
            showDialog(
              context: context,
              builder: (_) => new AlertDialog(
                title: Text("hello"),
                content: Text("something went wrong"),
              ),
            );
          } else if (jsonDecode(val.body)[0]["response"] == "true") {
            setState(() {
              isLoading = false;
              //isAbsorbing = false;
            });
            print(val.body);
            print("hotel added successfully");
            showDialog(
                context: context,
                builder: (_) => new AlertDialog(
                      title: Text("Hotel Added Successfully"),
                      content: Text("Hey there! Welcome to j1"),
                    ));
          }
        }).catchError((error) {
          setState(() {
            isLoading = false;
          });
          showDialog(
              context: context,
              builder: (_) {
                return new AlertDialog(
                    title: Text("error!"), content: Text(error.toString()));
              });
        });
      }
    } on SocketException catch (_) {
      setState(() {
        isLoading = false;
      });
      showDialog(
          context: context,
          builder: (_) => new AlertDialog(
                title: Text(
                  "Connection Error !",
                  style: TextStyle(
                      color: Colors.redAccent, fontWeight: FontWeight.bold),
                ),
                content: Text("Check your internet connection !"),
              ));
    }
  }

postFormData方法如下

  Future<dynamic> postFormData() async {
      //there are declarations for parent and headers I just excluded them because they were quite a lot ^_^ !
      final response = await http
        .post("https://thej1.app/apiDataReq",
            body: parent, headers: httpHeaders)
        .then((val) => val)
        .timeout(Duration(seconds: 5))
        .catchError((_) {
      setState(() {
        isLoading = false;
      });
      showDialog(
          context: context,
          builder: (_) =>
              new AlertDialog(title: Text("error"), content: Text("error")));
    });
    return response;
  }

还有其他相同的做法吗?和未来的建设者一样吗?我的响应主体具有一个结构,因此我可以创建响应模型。但是我可以将其隐含在将来的生成器中。我还希望捕获异常和超时错误。

感谢进阶!

我正在编写一个用于酒店注册的应用程序,在这里我发送如下checkNetworkConnection之类的发布请求只是为了检查id设备上是否存在活动连接,如果它为true,那么我正在致电...

flutter dart http-post
1个回答
0
投票

您如何看待我目前的实施情况?可以吗或者我真的想更改代码。我们可以通过未来的构建者实现这一目标吗?

© www.soinside.com 2019 - 2024. All rights reserved.