如何通过if语句在“ flutter / dart”中发送https发布请求

时间:2019-08-29 16:56:36

标签: api flutter https dart

我正在编写一个跨平台的二维扫描仪付款应用程序。我想将我的扫描值发送到需要json主体的第三方服务器api。如何制作一条if语句来发送我的api发布请求?

我尝试结合qr值作为关键来实现if / then(如果qrValue.isNotEmpty == true),但是从这一点上我完全迷失了。对于这个问题,我似乎找不到能理解的信息来源。

尤其是带有api帖子调用

我需要发送到的网址是:https://api.ext.payconiq.com/v3/payments

邮递员告诉我,正文和标题正确///

///

var qrValue = ""; 
  var amount = "1";
  var callbackurl = "api.vendingshop.nl:1880/ui/rest/payments";
  var description = "Test Payment for opening lock";
  var reference = "1";


 if ( qrValue.isNotEmpty == true){

  /* Here needs to come the value for the api input to send and receive the api calls to payconiq*/

  }


  /*



/* Here comes the "Headers"
content type: application / json
cache control: no cache
Authorization: /*apicode*/

"body"
{
         "amount" : "1",
         "callbackurl": "api.vendingshop.nl:1880/ui/rest/payments",
         "description": "Test payment for opening lock",
         "reference": "1"}

 ;
 }
}*/

////

1 个答案:

答案 0 :(得分:0)

Dart凭借其强大的http库,特别适合快速有效地执行此类操作。

您要做的就是将dart文件中的http库导入并通过异步功能执行发布操作。

import 'dart:http' as http;
[...]
void sendInfo({String amount,String description,String reference,String callbackurl})
 async{
 http.post(
 callbackurl,
 body: json.encode(
   {
     "amount" : amount,
     "description": description,
     "reference": reference
   }
 )     
 ).then((response){
print("Response Code : ", response.statusCode);
print("Response Body : ", response.body);
// Perform the required operation(s)
 });
}