Chris Benson
Chris Benson

Reputation: 29

Flutter How to send Http (post) Request using WorkManager Plugin

Hello Guys any help will be apprecited please,

I am unable to send Http post or get request using workmanager plugin in flutter, any solutions to this would be highly appreciated, thanks

Here is my code any help will be appreciated thanks

 Workmanager.executeTask((task, inputData) async {
switch (task) {
  case fetchBackground:
    print('checkStatusnow');
    final sharedPref = await SharedPreferences.getInstance();
    pendingStat = sharedPref.getBool('pendingStat');
    print('pendingStat $pendingStat');
   // await initialStat();

    String url = 'https://getStat.com/chargeStat';

      try {
    var param = {
      'authorization_code': authoStatCode,
      'email': umail,
      'amount': StatFare *100,
      
    };
    String body= json.encode(param);
    var response = await http.Client().post(Uri.parse(url), headers: <String, String>{
      'Authorization': StatKey,
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },body:  body,
    );
    if (response.statusCode == 200) {

     
      print(response.body);
      print("Successfull");


      final data = jsonDecode(response.body);
       print(data);
   

      if (StatFounds == null) {
        print("Status Not found");
      }
      else {
        print ('checkForSta');

      }

    }

    else {
      print(response.reasonPhrase);
      print("not available");
    

      sharedPref.setBool("Stat", true);

    }

  } catch (e) {

  }

Upvotes: 0

Views: 800

Answers (0)

Related Questions