1

I have a flutter application that needs to use webview in both the Android/Ios version and the Web version.

Essinatly to do this we need a code that separates the calls for every platform, and here is how I did it :

if (kIsWeb) {
  WebView.platform = WebWebViewPlatform();
  await Firebase.initializeApp(
    options: const FirebaseOptions(
      apiKey: "A**********************",
      appId: "**********************",
      messagingSenderId: "************",
      projectId: "water-maps-152c9",
    ),
  );
} else {
  if (Platform.isIOS) {
    await Firebase.initializeApp(
        options: const FirebaseOptions(
            apiKey: "*****************",
            appId: "1******************",
            messagingSenderId: "****************",
            projectId: "water-maps-152c9"));
    WebView.platform = AndroidWebView();
  } else {
    await Firebase.initializeApp();
    WebView.platform = AndroidWebView();
  }
}

the problem is when it runs on a spicific platform as android, it fails to import the webview for web package, also if I didn't import it I get a not defined error at WebView.platform = WebWebViewPlatform();

so I have tried to import it like this :

import 'package:webview_flutter/webview_flutter.dart' if (dart.library.html) 'package:webview_flutter_web/webview_flutter_web.dart';

but still am facing the `not defined` error as shown in the image:

enter image description here

Also when I try to import both as :

import 'package:webview_flutter/webview_flutter.dart' ;
import 'package:webview_flutter_web/webview_flutter_web.dart';

it gives me this error when I try to run the app on android

../src/flutter/.pub-cache/hosted/pub.dartlang.org/webview_flutter_web-0.1.0+4/lib/webview_flutter_web.dart:70:9: Error: Type 'IFrameElement' not found.
  final IFrameElement _element;
        ^^^^^^^^^^^^^
../src/flutter/.pub-cache/hosted/pub.dartlang.org/webview_flutter_web-0.1.0+4/lib/webview_flutter_web.dart:274:10: Error: Type 'HttpRequest' not found.
  Future<HttpRequest> request(String url,
         ^^^^^^^^^^^
../src/flutter/.pub-cache/hosted/pub.dartlang.org/webview_flutter_web-0.1.0+4/lib/webview_flutter_web.dart:281:21: Error: Type 'ProgressEvent' not found.
      void Function(ProgressEvent e)? onProgress}) {
                    ^^^^^^^^^^^^^
../src/flutter/packages/flutter_web_plugins/lib/src/navigation/js_url_strategy.dart:79:48: Error: Type 'html.EventListener' not found.
  external ui.VoidCallback addPopStateListener(html.EventListener fn);
                                               ^^^^^^^^^^^^^^^^^^
../src/flutter/packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart:193:3: Error: Type 'html.Location' not found.
  html.Location get _location => html.window.location;
  ^^^^^^^^^^^^^
../src/flutter/packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart:195:3: Error: Type 'html.History' not found.
  html.History get _history => html.window.history;
  ^^^^^^^^^^^^
../src/flutter/packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart:198:28: Error: Type 'html.EventListener' not found.
  void addPopStateListener(html.EventListener fn) {
                           ^^^^^^^^^^^^^^^^^^
../src/flutter/packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart:203:31: Error: Type 'html.EventListener' not found.
  void removePopStateListener(html.EventListener fn) {
                              ^^^^^^^^^^^^^^^^^^
../src/flutter/packages/flutter_web_plugins/lib/src/navigation/utils.dart:7:7: Error: Type 'AnchorElement' not found.
final AnchorElement _urlParsingNode = AnchorElement();
      ^^^^^^^^^^^^^
../src/flutter/packages/flutter_web_plugins/lib/src/navigation/utils.dart:20:7: Error: Type 'Element' not found.
final Element? _baseElement = document.querySelector('base');
      ^^^^^^^
../src/flutter/.pub-cache/hosted/pub.dartlang.org/webview_flutter_web-0.1.0+4/lib/shims/dart_ui_fake.dart:19:26: Error: Type 'html.Element' not found.
      String viewTypeId, html.Element Function(int viewId) viewFactory) {
                         ^^^^^^^^^^^^
../src/flutter/.pub-cache/hosted/pub.dartlang.org/webview_flutter_web-0.1.0+4/lib/webview_flutter_web.dart:23:25: Error: The method 'IFrameElement' isn't defined for the class 'WebWebViewPlatform'.
 - 'WebWebViewPlatform' is from 'package:webview_flutter_web/webview_flutter_web.dart' ('../src/flutter/.pub-cache/hosted/pub.dartlang.org/webview_flutter_web-0.1.0+4/lib/webview_flutter_web.dart').
3
  • can you check the solution by removing if() condition in the import statement and importing both the statements? Nov 8, 2022 at 8:27
  • Yes it gives an error in run time, as webview_flutter_web can't be imported in the android platform Nov 8, 2022 at 8:33
  • since WebWebViewPlatform is used in web and AndroidWebView in android, it requires both the import statements. but that doesn't work as plugin "webview_flutter_web" is only for web. hence throws the error on android or ios. it would have worked if they had the same class names. Nov 8, 2022 at 9:01

1 Answer 1

1

Solution from the official docs:

register_web_webview.dart

void registerWebViewWebImplementation() {
  WebView.platform = WebWebViewPlatform();
  await Firebase.initializeApp(
    options: const FirebaseOptions(
      ...yourOptions
    ),
  );
}

register_native_webview.dart

void registerWebViewWebImplementation() {
  if (Platform.isIOS) {
    await Firebase.initializeApp(
        options: const FirebaseOptions(
          ...yourOptions
        )
    );
    WebView.platform = AndroidWebView();
  } else {
    await Firebase.initializeApp();
    WebView.platform = AndroidWebView();
  }
}

main.dart

import 'register_native_webview.dart'
    if (dart.library.html) 'register_web_webview.dart';

main() {
  registerWebViewWebImplementation();
}
1
  • 1
    Please be careful of using actual api keys in the example to answer the questions. Nov 8, 2022 at 9:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.