0

i trying to create post function in flutter here is my code:

  Future<String> makePostRequest() async {
    String requestResult;
    var body = this.toMapRequest();
    String requestUrl = RequestZarinpal.PAYMENT_REQUEST_URL;
    String gateWayUrl;
    String jsonBody = json.encode(body);
    final encoding = Encoding.getByName('utf-8');

    Response response = await post(
      requestUrl,
      headers: headers,
      body: jsonBody,
      encoding: encoding,
    ).timeout(const Duration(seconds: 10), onTimeout: () {
      throw TimeoutException('The connection has timed out, Please try again!');
    });

    responseBody = response.body;
    var parsedJson = json.decode(responseBody);
    var data = parsedJson['data'];
    var error = parsedJson['errors'];
    if (error.toString() != "[]") {
      var errorcode = parsedJson['errors']['code'];
      print("$body   va  $requestUrl va $parsedJson");
      requestResult = "   شما ارور زیر را دریافت کرده اید \n$error";
    } else if (data.toString() != "[]") {
      var authority = parsedJson['data']['authority'];
      requestResult = "اتوریتی شما با موفقیت ساخته شد و به درگاه متصل می شود";
      _request.setAuthority(authority);
      print(parsedJson);

      String gateWay = RequestZarinpal.PAYMENT_GATEWAY_URL;
      gateWayUrl = "$gateWay$authority";

      if (await canLaunch(gateWayUrl)) {
        await launch(
          gateWayUrl,
          forceSafariVC: false,
          forceWebView: false,
          headers: headers,
        );
      } else {
        throw 'Could not launch $requestUrl';
      }

      print(requestResult);

      return requestResult;
    }
  }

but i got this error: The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type. Try adding either a return or a throw statement at the end.dart(body_might_complete_normally) what should i do?

4

2 回答 2

0

您缺少 if 条件中的 return 语句

if (error.toString() != "[]") {
  var errorcode = parsedJson['errors']['code'];
  print("$body   va  $requestUrl va $parsedJson");
  requestResult = "   شما ارور زیر را دریافت کرده اید \n$error";

  return requestResult;
}
于 2021-07-09T12:30:37.163 回答
0

使用这条线if (error.toString() != "[]"),您将您的功能划分为 2 个可能的结果。

正面的,没有return

也许您应该return requestResult;在大括号之后移动,以便无论如何都会触发返回。

于 2021-07-09T12:28:51.430 回答