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