0

I'm using the old version of Flutter_webview(version 3.0.4). I have pass my data to webview and it works well. But now I want to use the latest version, which is webview_flutter: ^4.7.0. How do I pass the data to the new version?

Old version

WebView(initialUrl: snapshot.data!.authorizationUrl!, //This is the data I want to pass to the new version
        javascriptMode: JavascriptMode.unrestricted,)

New Version

controller = 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'));

How do I pass the data to the new version?

1 Answer 1

0

According to the Migrating from 3.0 to 4.0 section in the documentation

Migrating from 3.0 to 4.0 Instantiating WebViewController In version 3.0 and below, WebViewController could only be retrieved in a callback after the WebView was added to the widget tree. Now, WebViewController must be instantiated and can be used before it is added to the widget tree. See Usage section above for an example.

Replacing WebView Functionality The WebView class has been removed and its functionality has been split into WebViewController and WebViewWidget.

WebViewController handles all functionality that is associated with the underlying web view provided by each platform. (e.g., loading a url, setting the background color of the underlying platform view, or clearing the cache).

WebViewWidget takes a WebViewController and handles all Flutter widget related functionality (e.g., layout direction, gesture recognizers).

See the Dartdocs for WebViewController and WebViewWidget for more details.

Here a sample that you can use

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: WebViewExample(),
    );
  }
}

class WebViewExample extends StatefulWidget {
  @override
  _WebViewExampleState createState() => _WebViewExampleState();
}

class _WebViewExampleState extends State<WebViewExample> {
  late WebViewController webViewController;

  @override
  void initState() {
    super.initState();
    webViewController = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Color(0x00000000))
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {},
          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'));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('WebView Example'),
      ),
      body: WebViewWidget(
        controller: webViewController,
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you but I don't think you understand my question. I want to pass this data (snapshot.data!.authorizationUrl!) which navigate me to a web so I can continue my thing from there. With the older version I could just pass it with the "initialUrl" method and this works well. Where do I pass the same data with the newer version so it can navigate me to where I want it to
Have you tried to put your url in the method loadRequest() like that: ..loadRequest(Uri.parse(snapshot.data!.authorizationUrl!)); ?
Yes I did but that didn't work
What error messages or console logs appear when loading the URL with loadRequest()? What exactly is happening with loadRequest(): blank page or ignored URL? Are you experiencing this problem with all URLs or only with snapshot.data!.authorizationUrl!?
The thing is that it doesn't return anything. I tried to debugPrint error yet no error is printed. Unlike the old version which takes me to where I want it to

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.