How do I link http url to flutter-web
Asked Answered
P

4

6

I'm learning flutter web.
I'm trying to open another url when button is clicked. Is there any way like this:
onclick: ()=>openurl("https://test.com")
How can I achieve this?
Please help

Playbook answered 9/6, 2019 at 4:43 Comment(0)
H
12

UPDATE: This is a VERY old "issue" from Flutter Web and has already been resolved using the "url_launcher" package

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
const String _url = 'https://flutter.dev';

void main() => runApp(
      const MaterialApp(
        home: Material(
          child: Center(
            child: RaisedButton(
              onPressed: _launchURL,
              child: Text('Show Flutter homepage'),
            ),
          ),
        ),
      ),
    );

void _launchURL() async {
  if (!await launch(_url)) throw 'Could not launch $_url';
}

Current easiest way to do it is by using href with your html library:

import 'dart:html' as html;

html.window.location.href = "https://www.google.com" // or any website your want

Put that code inside your onTap method and that's it.

Hostility answered 26/6, 2019 at 15:27 Comment(1)
how to navigate to another page with post parameters, "html.window.location.href" only works with get parametersVertebral
G
3

You can now use url_launcher just like for other platforms, web support has been added.

https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher_web

Guarneri answered 26/12, 2019 at 0:52 Comment(2)
how open in same tab?Alda
got it webOnlyWindowName:'_self',Alda
S
2

Flutter Web does not support plugins (yet), so you have to use replacements from dart:html

https://api.dartlang.org/stable/2.4.0/dart-html/Window/open.html window.open(url, 'tab');

or

https://api.dartlang.org/stable/2.4.0/dart-html/Window/location.html window.location.assign(url);

Sanborne answered 18/6, 2019 at 20:52 Comment(0)
M
-3

There is a plugin to achieve this: https://pub.dartlang.org/packages/url_launcher

Import the plugin;

import 'package:url_launcher/url_launcher.dart';

openURL() async {
  const url = 'https://test.com';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

Call the method

Medeiros answered 9/6, 2019 at 4:49 Comment(4)
I am not being able to import dart plugins. I get this error: Because webapp depends on url_launcher >=3.0.0 which requires the Flutter SDK, version solving failed. Flutter users should run flutter packages get instead of pub get.Playbook
please find more details about this, pub.dev/packages/url_launcherMedeiros
I am not being able to import native flutter plugins in flutter web.Playbook
flutter web does not support plugins right now. Any plugins.Reiser

© 2022 - 2024 — McMap. All rights reserved.