flutter_webview_plugin icon indicating copy to clipboard operation
flutter_webview_plugin copied to clipboard

_onStateChanged listener is only once?

Open shinriyo opened this issue 7 years ago • 3 comments

I wrote the example. https://pub.dartlang.org/packages/flutter_webview_plugin#-example-tab-

I pushed "Open widget webview" button. The example show log text onStateChanged: WebViewState.startLoad and onStateChanged: WebViewState.finishLoad in the bottom.

I pushed "Open widget webview" button again, but it didn't show log text again. Why only once?

I just want to do is that call JS when WebviewScaffold's url finish.

shinriyo avatar Mar 04 '18 14:03 shinriyo

I'm in the same scenario. I listen for urls with https protocol, and the first on it finds it catches and reloads the web view with it. After that all incoming urls are ignored.

I tried re-adding the listener, close and re-launch the flutterVebviewPlugin and even disposing of it an re-initiating it. None makes a difference.

The only way I manage to reset it is by reseting the app.

joelbrostrom avatar Jul 15 '19 11:07 joelbrostrom

Ok, there seams to be away around this. Set the onStateChanged listener INSIDE or your BUILD method so that it rebuilds every time your Web view does. In my case I was using BLoC and tried setting it in the build method, but above the block builder, which resulted in it only being rebuilt when the block class itself was.

@override
  Widget build(BuildContext context) {
    final HomeBloc _homeBloc = BlocProvider.of<HomeBloc>(context);
    // my_webview_provider.addListenerWithBloc(_homeBloc); <--- BAD: setting .onSatusChanged build is not enough when using BLoC, since only the BLoC scope will rebuild when possible.
    return BlocBuilder<HomeEvent, HomeBlocState>(
      bloc: _homeBloc,
      builder: (BuildContext context, HomeBlocState state) {
        my_webview_provider.addListenerWithBloc(_homeBloc); <--- GOOD: setting .onSatusChanged inside of BlocBuilder scope.

        if (state is HomeWebview) {
          my_webview_provider.reloadWithUrl(_tabUrls[_currentTabIndex]);
    ...
    ...

joelbrostrom avatar Jul 15 '19 11:07 joelbrostrom

same issue

tegarkurniawan avatar Jun 10 '21 08:06 tegarkurniawan