The method 'SurfaceAndroidWebView' isn't defined for the class '_WebViewExampleState'

12.4k views Asked by At

I tried to run an example I got from https://pub.flutter-io.cn/packages/webview_flutter/example to use webview in flutter. But I have this kind of error:

lib/main.dart:43:48: Error: The method 'SurfaceAndroidWebView' isn't defined for the class '_WebViewExampleState'. - '_WebViewExampleState' is from 'package:webview_test/main.dart' ('lib/main.dart'). Try correcting the name to the name of an existing method, or defining a method named 'SurfaceAndroidWebView'. if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView(); ^^^^^^^^^^^^^^^^^^^^^

And my code had an error in SurfaceAndroidWebView: enter image description here

Here is my pubspec.yaml:

name: webview_test
description: A new Flutter application.

publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  webview_flutter:
  cupertino_icons: ^0.1.3

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true
  assets:
    - assets/sample_audio.ogg

Thanks in advance!

4

There are 4 answers

1
alfianakbar On BEST ANSWER

After several hours of a workaround, finally, I got the answer. I have to upgrade my flutter to the latest one. And then upgrade my pubspec.yaml.

1
Akif On

You need to import SurfaceAndroidWebView page.


import './SurfaceAndroidWebView.dart';

0
Cyrus the Great On

Android minSdkVersion should be 19:

add the following to /android/app/build.gradle:

android {
    defaultConfig {
        // Required by the Flutter WebView plugin.
        minSdkVersion 19
    }
  }
0
Will Gardner On

Sweet! I had the same problems and figured it out. I had an older version of webview_flutter defined in my pubspec.yaml. So, when I tried to do a flutter pub get, it was not getting the latest version of webview I think.

In pubspec.yaml I had this (old version): webview_flutter: ^0.3.22+1

I changed to this (most recent version as of this post): webview_flutter: ^1.0.7

Then I went to console to run: flutter pub get

And now my app recognizes SurfaceAndroidWebView !

I'm a newbie when it comes to this so maybe others already have tried this, but maybe others didn't know.