Repository: flutter
Issue: PlatformException when trying to create flutter webview in existing app using FlutterFragment, Trying to create a platform view of unregistered type: plugins.flutter.io/webview
Labels: c: crash,platform-android,a: existing-apps,a: platform-views,p: webview,package,has reproducible steps,P2,found in release: 2.2,found in release: 2.5,team-android,triaged-android

An error occurs when trying to create a **FlutterFragment** from a native android application:

_MainActivity.java:_
```
   private void prepareButton(MainActivity context) {
        final String link = "http://flutter.dev";

        mInvokeFlutterView = findViewById(R.id.invoke_flutter_view);
        mInvokeFlutterView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                prepareEngine(link);
            }
        });
    }

    private void prepareEngine(String link) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        if (fragmentManager != null) {
           flutterFragment = (FlutterFragment) fragmentManager
                    .findFragmentByTag(TAG_FLUTTER_FRAGMENT);
            if (flutterFragment == null) {
                flutterFragment = FlutterFragment.withNewEngine()
                        .initialRoute(link)Trace log:Trace log:
                        .build();
                fragmentManager
                        .beginTransaction()
                        .replace(
                                R.id.flutter_view,
                                flutterFragment,
                                TAG_FLUTTER_FRAGMENT
                        )
                        .commit();
            }
        }
    }
```
_main.dart:_
```
import 'dart:ui';
import 'package:flutter/material.dart';
import 'web_view_container.dart';

void main() {
  print ("main()->${window.defaultRouteName}");
  runApp(chooseWidget(window.defaultRouteName));
}

Widget chooseWidget(String url) {
  return FlutterActivityWrapper(url);
}

class FlutterActivityWrapper extends StatefulWidget {
  String _link;
  FlutterActivityWrapper(this._link);

  @override
  createState() => _FlutterActivityWrapperState();

}

class _FlutterActivityWrapperState extends State<FlutterActivityWrapper> {
  @override
  void initState() {
    print('_FlutterActivityWrapperState.initState');
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: WebViewContainer(widget._link /*widget.__link*/),
    );
  }
}
```
_web_view_container.dart:_
```
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'dart:async';

class WebViewContainer extends StatefulWidget {
  final  url;

  WebViewContainer(this.url){
    print ('WebViewContainer->$url');
  }

  @override
  createState() => _WebViewContainerState(this.url);
}

class _WebViewContainerState extends State<WebViewContainer> {
  var _url;
  bool _progress = false;
  final _key = UniqueKey();
  _WebViewContainerState(this._url);
  WebViewController _controller;webview_flutter: ^ 0.3.22 + 1
  bool _enableJS = true; //true;

  final Completer<WebViewController> _controllerCompleter =
        Completer<WebViewController>();

  @override
  void initState() {
    print('_WebViewContainerState.initState');
    super.initState();
  }

  Future<void> _onWillPop(BuildContext context) async {
    print("onwillpop");
    if (await _controller.canGoBack()) {
      _controller.goBack();
    } else {
      setState(() {
        _enableJS = false;
      });

      SystemChannels.platform.invokeMethod<void>('SystemNavigator.pop');

      return Future.value(false);
    }
  }

  @override
  Widget build(BuildContext context) {
    print ("_WebViewContainerState.build->$_url");
    return
      WillPopScope(
        onWillPop: () => _onWillPop(context),
      child:
      Scaffold(
       appBar:

      AppBar(
        titleSpacing: 0.0,
        title: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  IconButton(
                    icon: Icon(Icons.keyboard_backspace),
                    onPressed: () {
                      _onWillPop(context);
                    }
                  ),
                  Text ("FLUTTER WEB VIEW"),
                ]
              ),
        ),

        body: Column(
          children: [
            Center(
              child:
              _progress ?
              new SizedBox(
                height: 64.0,
                width: 64.0,
                child: new CircularProgressIndicator(
                  value: null,
                  strokeWidth: 8.0,
                ),
              )
              :
              SizedBox.shrink()
            ),
            Expanded(
                child: WebView(
                  key: _key, javascriptMode: _enableJS ? JavascriptMode.unrestricted : JavascriptMode.disabled, initialUrl: _url,
                    onWebViewCreated: _onWebViewCreated, onPageFinished: _onPageFinished, )
            )
          ],
        ),
        ));
  }

  void _onWebViewCreated(WebViewController controller) {
    print("------- OnWebViewCreated -------");
    _controllerCompleter.future.then((value) => _controller = value);
    _controllerCompleter.complete(controller);
    setState(() {
      _progress = true;
    });
  }

  void _onPageFinished(String url) {
    print("------- _onPageFinished -------");
    setState(() {
      _progress = false;
    });
  }
}
```
Used by **webview_flutter: ^ 0.3.22 + 1**

_Trace log:_
```
I/flutter: main()->http://flutter.dev
I/flutter: _FlutterActivityWrapperState.initState
I/flutter: WebViewContainer->http://flutter.dev
I/flutter: _WebViewContainerState.initState
I/flutter: _WebViewContainerState.build->http://flutter.dev
E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: Trying to create a platform view of unregistered type: plugins.flutter.io/webview
        at io.flutter.plugin.platform.PlatformViewsController$1.createPlatformView(PlatformViewsController.java:97)
        at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.create(PlatformViewsChannel.java:95)
        at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.onMethodCall(PlatformViewsChannel.java:59)
        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:226)
        at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
        at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:631)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:326)
        at android.os.Looper.loop(Looper.java:170)
        at android.app.ActivityThread.main(ActivityThread.java:6991)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:884)
    , null)
    #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
    #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
    <asynchronous suspension>
    #2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
    #3      AndroidViewController._create (package:flutter/src/services/platform_views.dart:633:54)
    #4      AndroidViewController.setSize (package:flutter/src/services/platform_views.dart:550:14)
    #5      RenderAndroidView._sizePlatformView (package:flutter/src/rendering/platform_view.dart:175:29)
    #6      RenderAndroidView.performResize (package:flutter/src/rendering/platform_view.dart:156:5)
    #7      RenderObject.layout (package:flutter/src/rendering/object.dart:1746:9)
    #8      RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:111:13)
    #9      RenderObject.layout (package:flutter/src/rendering/object.dart:1767:7)
    #10     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:111:13)
    #11     RenderObject.layout (package:flutter/src/rendering/object.dart:1767:7)
    #12     RenderFlex.performLayout (package:flutter/src/rendering/flex.dart:808:17)
    #13     RenderObject.layout (package:flutter/src/rendering/object.dart:1767:7)
    #14     MultiChildLayoutDelegate.layoutChild (package:flutter/src/rendering/custom_layout.dart:171:11)
    #15     _ScaffoldLayout.performLayout (package:flutter/src/material/scaffold.dart:484:7)
    #16     MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:240:7)
    #17     RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:399:14)
    #18     RenderObject.layout (package:flutter/src/rendering/object.dart:1767:7)
    #19     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:111:13)
    #20     RenderObject.layout (package:flutter/src/rendering/object.dart:1767:7)
    #21     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:111:13)
    #22     _RenderCustomClip.performLayout (package:flutter/src/rendering/proxy_box.dart:1248:11)
    #23     RenderObject.layout (package:flutter/src/rendering/object.dart:1767:7)
    #24     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:111:13)
    #25     RenderObject.layout (package:flutter/src/rendering/object.dart:1767:7)
    #26     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:111:13)
    #27     RenderObject.layout (package:flutter/src/rendering/object.dart:1767:7)
    #28     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:111:13)
    #29     RenderObject.layout (package:flutter/src/rendering/object.dart:1767:7)
    #30     RenderProxyBoxMixin.performLayout (packa
```
_Flutter doctor:_
```
	micrcx@micrcx-desktop:~/AndroidStudioProjects/TestFlutterView/web_viewer$ ./../../../flutter/bin/flutter doctor -v
[✓] Flutter (Channel stable, v1.17.3, on Linux, locale en_IL)
    • Flutter version 1.17.3 at /home/micrcx/flutter
    • Framework revision b041144f83 (2 weeks ago), 2020-06-04 09:26:11 -0700
    • Engine revision ee76268252
    • Dart version 2.8.4

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /home/micrcx/Android/Sdk
    • Platform android-29, build-tools 29.0.3
    • Java binary at: /home/micrcx/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Android Studio (version 4.0)
    • Android Studio at /home/micrcx/android-studio
    • Flutter plugin version 46.0.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.46.1)
    • VS Code at /usr/share/code
    • Flutter extension version 3.9.1

[✓] Connected device (1 available)
    • LG H870 • LGH870f90ccfe • android-arm64 • Android 9 (API 28)

• No issues found!
```
**PS.The detected error maybe is close to the bug https://github.com/flutter/flutter/issues/25714, but appears in a different context.**
PPS. The most interesting thing is that the same code when using **FlutterActivity** is executed without problems.