skip to Main Content

I have a problem with my flutter app using the package webview_flutter , here Is my code:

pubspec.yaml

  webview_flutter: ^4.0.6

dart

  WebViewController breachWebViewController = WebViewController()
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setBackgroundColor(const Color(0x00000000))
    ..setNavigationDelegate(
      NavigationDelegate(
        onProgress: (int progress) {
          // Update loading bar.
        },
        onPageStarted: (String url) {},
        onPageFinished: (String url) {},
        onWebResourceError: (WebResourceError error) {},
        onNavigationRequest: (NavigationRequest request) {
          if (request.url.startsWith('https://www.youtube.com/')) {
            return NavigationDecision.prevent;
          }
          return NavigationDecision.navigate;
        },
      ),
    )
    ..loadRequest(Uri.parse('https://flutter.dev'));

but I am getting this error :

The class 'WebViewController' doesn't have an unnamed constructor.
Try using one of the named constructors defined in 'WebViewController'.

2

Answers


  1. I just got this problem as a result of someone else having upgraded webview_flutter to a newer version.
    (The constructor changed in the major version change from 3 to 4 for this module).

    To update this dependency to the correct version in my environment I needed to do:

    flutter pub upgrade
    

    (And in my case it was a sub module, not the main app – I needed to go into the module that used webview_flutter and run that command there)


    Login or Signup to reply.
  2. Based on the library example project you have to initialize the controller in this way, taken from here https://pub.dev/packages/webview_flutter/example:

    late final WebViewController _controller;
    
      @override
      void initState() {
        super.initState();
    
        // #docregion platform_features
        late final PlatformWebViewControllerCreationParams params;
        if (WebViewPlatform.instance is WebKitWebViewPlatform) {
          params = WebKitWebViewControllerCreationParams(
            allowsInlineMediaPlayback: true,
            mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
          );
        } else {
          params = const PlatformWebViewControllerCreationParams();
        }
    
        final WebViewController controller =
            WebViewController.fromPlatformCreationParams(params);
        // #enddocregion platform_features
    
        controller
          ..setJavaScriptMode(JavaScriptMode.unrestricted)
          ..setBackgroundColor(const Color(0x00000000))
          ..setNavigationDelegate(
            NavigationDelegate(
              onProgress: (int progress) {
                debugPrint('WebView is loading (progress : $progress%)');
              },
              onPageStarted: (String url) {
                debugPrint('Page started loading: $url');
              },
              onPageFinished: (String url) {
                debugPrint('Page finished loading: $url');
              },
              onWebResourceError: (WebResourceError error) {
                debugPrint('''
    Page resource error:
      code: ${error.errorCode}
      description: ${error.description}
      errorType: ${error.errorType}
      isForMainFrame: ${error.isForMainFrame}
              ''');
              },
              onNavigationRequest: (NavigationRequest request) {
                if (request.url.startsWith('https://www.youtube.com/')) {
                  debugPrint('blocking navigation to ${request.url}');
                  return NavigationDecision.prevent;
                }
                debugPrint('allowing navigation to ${request.url}');
                return NavigationDecision.navigate;
              },
            ),
          )
          ..addJavaScriptChannel(
            'Toaster',
            onMessageReceived: (JavaScriptMessage message) {
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(content: Text(message.message)),
              );
            },
          )
          ..loadRequest(Uri.parse('https://flutter.dev'));
    
        // #docregion platform_features
        if (controller.platform is AndroidWebViewController) {
          AndroidWebViewController.enableDebugging(true);
          (controller.platform as AndroidWebViewController)
              .setMediaPlaybackRequiresUserGesture(false);
        }
        // #enddocregion platform_features
    
        _controller = controller;
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search