Flutter url_launcher unable launch webview short google form link

5,154

I suggest you using 'flutter_webview_plugin' package instead of 'url_launcher'.

This problem is occurred by redirecting and browser can not understand starts of 'intent' in android.

enter image description here

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: _buildBody(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) =>
                  WebViewPage(url: 'https://forms.gle/mEwVA8jXmwJEFn5X6'),
            ),
          );
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  Widget _buildBody() {
    return Container();
  }
}

class WebViewPage extends StatefulWidget {
  final String url;
  WebViewPage({Key key, this.url}) : super(key: key);

  @override
  _WebViewPageState createState() => _WebViewPageState();
}

class _WebViewPageState extends State<WebViewPage> {
  final flutterWebviewPlugin = FlutterWebviewPlugin();

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    flutterWebviewPlugin.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: WebviewScaffold(
        appBar: AppBar(
          title: Text('WebView Page'),
        ),
        url: widget.url,
        userAgent: 'Fake',
        clearCookies: false,
        clearCache: false,
        hidden: true,
        appCacheEnabled: true,
        supportMultipleWindows: true,
      ),
    );
  }
}


Share:
5,154
B.Cos
Author by

B.Cos

Thanks for visiting my profile.

Updated on December 17, 2022

Comments

  • B.Cos
    B.Cos over 1 year

    I was using flutter to develop a app with url_launcher 5.7.2 package https://pub.dev/packages/url_launcher that launch webivew only for the google form when user tap some button.

    However I find that if using the shorten URL of google form it will hit error of ERR_UNKNOWN_URL_SCHEME, using the original url is working.

    I was using this example https://pub.dev/packages/url_launcher/example and replace the url to https://forms.gle/mEwVA8jXmwJEFn5X6 and then click the button of Launch in app(JavaScript ON)

    await launch(url,forceSafariVC: true,
            forceWebView: true,
            enableJavaScript: true,);
    

    If using the original long URL https://docs.google.com/forms/d/e/1FAIpQLSfzXnHMRe890CJj5rSxN-jonjrvZ8HvRBSFcdyJD5IDhOr-IQ/viewform?usp=sf_link is working.

    I already add android:usesCleartextTraffic="true" to my AndroidManifest.xml but still not working for short url

  • B.Cos
    B.Cos over 3 years
    Hi, launch() can work but I need launch in webview using this await launch(url,forceSafariVC: true, forceWebView: true, enableJavaScript: true,);
  • KuKu
    KuKu over 3 years
    I changed launch method as you said. But it works well again.
  • B.Cos
    B.Cos over 3 years
    Maybe only iPhone can work? I test in Android but still not working ,
  • B.Cos
    B.Cos over 3 years
    Hi, I need forceWebView: true because i need use in app webview to hide the display URL to avoid user see the url
  • Jitesh Mohite
    Jitesh Mohite over 3 years
    where you are trying real device or on emulator?
  • B.Cos
    B.Cos over 3 years
    On Android real device , Android 9
  • KuKu
    KuKu over 3 years
    @B.Cos How about you use flutter_webview_plugin? I changed code with 'flutter_webview_plugin'.
  • B.Cos
    B.Cos over 3 years
    How can pass the URL to WebviewScaffold() instead of hard code the URL? Example read the textfield URL value and pass to WebviewScaffold()
  • B.Cos
    B.Cos over 3 years
    ok I get the idea, thanks for this alternative solution
  • B.Cos
    B.Cos over 3 years
    one quick question , why need to use userAgent: 'fake', ? I remove this or change to Chrome will hit back same error. But using fake userAgent is working
  • KuKu
    KuKu over 3 years
    If there is a normal 'userAgent' value at flutter_webview, flutter_webview will redirect https:// to intent://.