简体   繁体   中英

Platform Exception Flutter Webview, Load Url Failed

I have a code like below, on Android the following code runs fine, when I run the iOS application, I get an error like the following

PlatformException (PlatformException(loadUrl_failed, Failed parsing the URL, Request was: '{ headers = ""; url = "xxxxx; }', null))

What should I do? i use this plugin https://pub.dev/packages/webview_flutter loadUrl function can't run at IOS appication

class WebViewDefault extends StatefulWidget {
  final String targetUrl;

  const WebViewDefault({
    keys,
    @required this.targetUrl,
  }) : super(key: key);

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

class WebViewDefaultState extends State<WebViewDefault> {
  var url;
  WebViewController controller;

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

  @override
  Widget build(BuildContext context) {
    url = widget.targetUrl;
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();

    print("target URL ${widget.key} $url");
    return WebView(
      initialUrl: url,
      javascriptMode: JavascriptMode.unrestricted,
      onWebViewCreated: (webviewController) {
        controller = webviewController;
      },
      onProgress: (int progress) {
        print("WebView is loading(progress : $progress%)");
      },
      onPageStarted: (String url) {
        print('Page started loading: $url');
      },
      onPageFinished: (String url) {
         print('Page finished loading: $url');
      },
      gestureNavigationEnabled: true,
    );
  }

  void reloadNewUrl(url) {
    setState(() {
      controller.loadUrl(url);
    });
  }
}

Faced a similar issue recently. Converting the url to URI did the trick. Here is your code with a little tweak.

class WebViewDefault extends StatefulWidget {
  final String targetUrl;

  const WebViewDefault({
    keys,
    @required this.targetUrl,
  }) : super(key: key);

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

class WebViewDefaultState extends State<WebViewDefault> {
  var url;
  WebViewController controller;

  @override
  void initState() {
    super.initState();
    url = widget.targetUrl;
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
  }

  @override
  Widget build(BuildContext context) {
    print("target URL ${widget.key} $url");
    return WebView(
      initialUrl: Uri.parse(url).toString(),
      javascriptMode: JavascriptMode.unrestricted,
      onWebViewCreated: (webviewController) {
        controller = webviewController;
      },
      onProgress: (int progress) {
        print("WebView is loading(progress : $progress%)");
      },
      onPageStarted: (String url) {
        print('Page started loading: $url');
      },
      onPageFinished: (String url) {
         print('Page finished loading: $url');
      },
      gestureNavigationEnabled: true,
    );
  }

  void reloadNewUrl(url) {
    setState(() {
      controller.loadUrl(Uri.parse(url).toString());
    });
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM