-1

I have been trying to launch a URL insder the flutter app. I am using url_launcher but am unable to launch the URL. The URL that I am receiving is a response from an API.

If I am copying the response and sending it statically, it's working, but when passing as a dynamic variable, it always shows "Could not be launched."

Code:

  checkoutCartItem() async {
  var url = Uri.parse(
      '');
  final headers = {'Content-Type': 'application/json'};
  var body = jsonEncode({
    "status": 2,
    "uid": SignForm.userIdGlobal,
  });
  final encoding = Encoding.getByName('utf-8');

  Response response = await post(
    url,
    headers: headers,
    body: body,
    encoding: encoding,
  );
  print(response.body);

  Body.url = response.body.toString();
  print(Body.url);

  if (await canLaunch(Body.url.toString())) {
    await launch(Body.url.toString());
  } else {
    throw 'Could not launch $Body.url';
  }
}

Flutter version: 2.2,

url_launcher: ^6.0.4

Error:

[VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: Could not launch [TextElement: '
', LinkElement: 'https://www.bsestarmf.in/ClientOrderPayment.aspx?K4HhW6zSxVp2T2sl9n5acA+J8qCjHcdVy2hyQmgbsuje2e6rf0+sujJisssdsaBFQV1zicfCer4VQUqJtRxgRiLYXwfXKkOBj9pA5dqrlOiLEPkxgWpB0QQa36DMiHhyqCA/fP60nFus9nGlM='
print(response.body)

Gives:

https://www.bsestarmf.in/ClientOrderPayment.aspx?K4HhW6zSxVp2T2sl9n5acA+J8qCjHcdVy2hyQmgbsuje2e6rf0+sujJiBFQV1zicfCer4VQUqJtRxgRiLYXwfXKkOBj9dddddpA5dqrlOiLEPkxgWpB0QQa36DMiHhyqCA/fP60nFus9nGlM=
[VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: Could not launch Body.url
#0      checkoutCartItem (package:optymoney/Cart/Components/Body.dart:58:5)
<asynchronous suspension>
#1      _BodyState.build.<anonymous closure>.<anonymous closure> (package:optymoney/Cart/Components/Body.dart:326:29)
<asynchronous suspension>

This is a payment link that should open a browser inside the flutter app. But it's now working.

Please help!!!

N.B: Since this is a payment link, it has been edited and so it won't work if you try launching it from a browser

Rishabh
  • 29
  • 3
  • can you show the json response coming in body? – Nabin Dhakal Jun 04 '21 at 05:25
  • I have updated the original question, please check. – Rishabh Jun 04 '21 at 05:34
  • It says your url is null or invalid? I tested your url, it works fine. Did you debug your code? What is your Body class? Did the`Body.url = response.body.toString();` code work? What is the result of `print(Body.url);`. You have two `print` method. Which one prints the `https://www.bsestarmf.in/C...` – Reza M Jun 04 '21 at 06:19
  • The body.url is because I have defined is a global variable. The app works fine. I have other APIs defined. Both the print statements are printing the same thing. As you can see I have assigned Body.url as response.body. That is just for testing purposes. – Rishabh Jun 04 '21 at 06:28

1 Answers1

0
final payUrl = Uri.encodeFull(response.body);
  if (await canLaunch(payUrl)) {
    await launch(payUrl, forceWebView: true, enableJavaScript: true);
  } else {
    throw 'Could not launch $payUrl';
  }

This is how I resolved the issues. Turns out that encoding the received response was the solution needed.

Rishabh
  • 29
  • 3