How to open URL in flutter app with url_launcher? - flutter

I use the package url_launcher to open url, here is the code:
Future <void> _openURL(String url) async{
if(await canLaunch(url)) {
await launch(
url,
forceSafariVC: false,
forceWebView: true,
);
}
else{
throw 'Cant open URL';
}
}
ListTile(
title: Text('Google'),
onTap: () {
_openURL('https://www.google.de/');
}),
But no matter what url i want to open, i get the error 'Cant open URL'

I get the same error: Unhandled Exception: Could not launch
As you can see here https://pub.dev/packages/url_launcher you have to put the following snippet at your AndroidManifest.xml
<queries>
<!-- If your app opens https URLs -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https" />
</intent>
</queries>

Try to below code:
Create launchURL function :
_launch() async {
const url = 'https://www.google.de/';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Create your Widget :
ListTile(
onTap: _launch,
title: Text('Google'),
),
Hope its solved your problem

I was not able to open the URL in Android using Flutter app with pub file https://pub.dev/packages/url_launcher
Since JavaScript was not enable.
Here's working code snippet for Android -
void _launchURL(String _url) async {
if (await canLaunch(_url)) {
await launch(_url, forceSafariVC: true, forceWebView: true, enableJavaScript: true);
} else {
throw 'Could not launch $_url';
}
}
Then I added a key enableJavaScript: true and it started working on Android too. iOS no need for configuration.

Related

Flutter how to open pdf using web browser

How to open pdf from url address in flutter?
I try call window like this:
And select there application for open pdf file.
I try do it using libraries:
https://pub.dev/packages/pdfx
https://pub.dev/packages/url_launcher
But none of them cannot do it. Any ideas or samples?
You can force webview using
Future<void> _launchInBrowser(Uri url) async {
if (!await launchUrl(
url,
mode: LaunchMode.externalApplication,
)) {
throw 'Could not launch $url';
}
}
or to load it in an inapp web view
Future<void> _launchInWebViewOrVC(Uri url) async {
if (!await launchUrl(
url,
mode: LaunchMode.inAppWebView,
webViewConfiguration: const WebViewConfiguration(
headers: <String, String>{'my_header_key': 'my_header_value'}),
)) {
throw 'Could not launch $url';
}
}

method not available: ext.flutter.inspector.setPubRootDirectories

I am getting the error below when I try to download a file using flutter_downloader. My Google searches seem to indicate that this is a problem with Firebase. I have firebase_messaging installed but the issue only comes up when I try to downlaod a file using flutter_downloader.
Error handling 'serviceExtension' custom request: method not available: ext.flutter.inspector.setPubRootDirectories
Please assist.
Herewith the Flutter code snippet for the downloader;
IconButton(
icon: Icon(Icons.download),
onPressed: () async {
final status = await Permission.storage.request();
if (status.isGranted) {
final externalDir = await getExternalStorageDirectory();
final id = await FlutterDownloader.enqueue(
url: files[i].file,
fileName: 'filename',
savedDir: externalDir.path,
showNotification: true,
openFileFromNotification: true,
);
} else {
print('Permission denied');
}
},
),

How to resolve could not launch Error in Url launcher

I am trying open a link on Google pixel API 30 Emulator but keep on getting error. Have tried flutter clean, restarted app.
Dependency:-
url_launcher: ^6.0.3
Code:-
InkWell(
onTap:() => _launchURL("https://google.com"),
child: Image.asset("assets/images/googleIcon.jpg")
),
_launchURL(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Error:-
Unhandled Exception: Could not launch https://google.com
Add a URI class since url_launcher recommends encoding the URLs as URI. Link to Doc
final Uri _faireGitHubUri = Uri.https('google.com', '/');
InkWell(
onTap:() => _launchURL(_websiteUri.toString()),
child: Image.asset("assets/images/googleIcon.jpg")
),

How to open installed app with url_launcher else open in browser?

like the title suggests, I use url_launcher to open some websites when the user taps a selected icon. The issue is that it opens the browser website if the associated app is not installed on the device otherwise nothing happens if said app is installed. The app just doesn't respond. From what I've read, its supposed to open the associated app anyway? Or am I mistaken?
Here's the onTap:
GestureDetector(
onTap: _launchTwitchURL,
child: Image.asset(
'assets/images/icon_twitch.png', // On click should redirect to an URL
width: 40.0,
height: 40.0,
),
),
And here's the call:
Future<void> _launchTwitchURL() async {
const url = 'https://www.twitch.tv/example';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Using android-intent package like #iDecode suggested to get it to work. appavailability might be a better suited/cleaner package depending on your needs.
Future<void> _launchTwitchURL() async {
const url = 'https://www.twitch.tv/example';
if (await canLaunch(url)) {
await launch(url);
} else if (Platform.isAndroid) {
final AndroidIntent intent = AndroidIntent(
action: 'action_view',
data: 'https://www.twitch.tv/example', // replace com.example.app with your applicationId
);
await intent.launch();
} else {
throw 'Could not launch $url';
}
}
Thanks #iDecode

How to launch other android apps using the flutter app

I have used the following method as specified in the documentation.
Future<void> launchUniversalLink(String url) async {
if (await canLaunch(url)) {
final bool nativeAppLaunchSuccess = await launch(url, forceSafariVC: false, universalLinksOnly: true);
print(nativeAppLaunchSuccess);
}else {
print('launch not successfull');
}
}
if I give URL = 'https://www.WhatsApp.com'
print(nativeAppLaunchSuccess); output ==> true
but still the app launches in the browser.
can anyone help me with this problem
Oh I'm sorry. This is my mistake.
Please use 'device_apps' flutter package and usage is below.
And here is a how to know app package name.
https://www.techmesto.com/find-android-app-package-name/
In ios, you know other app's custom Url schema that officially opened.
But usually we can not know that url.
So below ios code is executed, it will open appstore page and need to push 'open' button.
if (Platform.isAndroid) {
if (await DeviceApps.isAppInstalled('com.nbt.moves') ==
true) {
DeviceApps.openApp('com.nbt.moves');
}
} else {
const url =
'https://apps.apple.com/kr/app/%EC%BA%90%EC%8B%9C%EC%8A%AC%EB%9D%BC%EC%9D%B4%EB%93%9C-%EC%8A%A4%ED%85%9D%EC%97%85/id1400703652?uo=4';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}