Flutter webview 连接在 web 中被拒绝

GHH*_*GHH 5 android webview flutter

Android我想在、IOS和上构建一个带有 WebView 的应用程序Web。

所以我下载了这个示例

当我加载“https://flutter.dev”时它起作用 在此输入图像描述

但是当我将网址更改为“https://www.google.com”时,网络会出现错误连接被拒绝。

模拟器仍然可以工作。 在此输入图像描述 如何解决

另外,您有什么建议库在Android、IOS和上构建 WebView 吗Web?

我尝试了几种流行的,但它们都很难使用。

Men*_*elG 0

请参阅Github 问题/文档:

  1. 不支持 iframe 嵌入的普通 URL(例如https://google.com,您需要使用SourceType.urlBypass它)。
 webviewController.loadContent(
                'https://google.com',
                SourceType.urlBypass,
              );
Run Code Online (Sandbox Code Playgroud)

完整示例:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyWebViewPage(),
    );
  }
}

class MyWebViewPage extends StatefulWidget {
  @override
  _MyWebViewPageState createState() => _MyWebViewPageState();
}

class _MyWebViewPageState extends State<MyWebViewPage> {
  late WebViewXController webviewController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('WebViewX Example'),
      ),
      body: Column(
        children: [
          WebViewX(
            initialContent: 'https://example.com',
            // initialSourceType: SourceType.,
            onWebViewCreated: (controller) => webviewController = controller,
            onWebResourceError: (error) {
              print('WebViewX Error: ${error.errorCode}, ${error.description}');
            },
            // ... other options

            width: 400,
            height: 400,
          ),
          ElevatedButton(
            onPressed: () {
              // Load a URL when the button is pressed
              webviewController.loadContent(
                'https://google.com',
                SourceType.urlBypass,
              );
            },
            child: Text('Load Google Website'),
          ),
          ElevatedButton(
            onPressed: () {
              // Go back in the webview's history
              webviewController.goBack();
            },
            child: Text('Go Back'),
          ),
          ElevatedButton(
            onPressed: () {
              // Go forward in the webview's history
              webviewController.goForward();
            },
            child: Text('Go Forward'),
          ),
        ],
      ),
    );
  }
}

Run Code Online (Sandbox Code Playgroud)

在此输入图像描述