How to send https post request in "flutter / dart" with an if statement

2,345

Dart is exceptionally good for quickly and efficiently implementing such operations, all thanks to its powerful http library.

All you have to do is import the http library in your dart file and perform a post operation from an async function.

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)
 });
}
Share:
2,345
Michiel Degruytere
Author by

Michiel Degruytere

Updated on December 13, 2022

Comments

  • Michiel Degruytere
    Michiel Degruytere over 1 year

    I'm coding a crossplatform qr-scanner payment app. I want to have my scanned value to be sent to a third party server api which needs a json body. How do I make an if statement that sends my api post request?

    I've tried implementing if/then in combination with the qr value being the key ( if qrValue.isNotEmpty == true ) but I'm completely lost from this point. I can't seem to find a source of infromation that I understand for this problem.

    Especially with an api post call

    the url I need to send this to is: https://api.ext.payconiq.com/v3/payments

    Postman has shown me that the body and headers are correct///

    ///

    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"}
    
     ;
     }
    }*/
    

    ////