How do I link http url to flutter-web

2019-08-22 03:12发布

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

3条回答
Rolldiameter
2楼-- · 2019-08-22 03:14

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.

查看更多
The star\"
3楼-- · 2019-08-22 03:14

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);

查看更多
孤傲高冷的网名
4楼-- · 2019-08-22 03:17

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

查看更多
登录 后发表回答