Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I link http url to flutter-web

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

like image 507
Mandip Avatar asked Dec 11 '22 02:12

Mandip


2 Answers

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.

like image 129
Mariano Zorrilla Avatar answered Dec 20 '22 03:12

Mariano Zorrilla


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

like image 21
Dariusz Bacinski Avatar answered Dec 20 '22 03:12

Dariusz Bacinski