scenario

The h5 page will fetch data from cookies, so it needs to insert a dozen of data into the cookies of the flutter WebView. I set it according to the usage mode checked online, but I found that only one was valid after catching the packet through Fiddler. I tried many times, but only one was valid

Later, I saw that the cookie setting data is similar to the key value pair inside the key value pair, and I had a brainwave. After transformation, I succeeded

Correct posture

The introduction of

  • Use the WebView plugin that is officially maintained by Flutter
webview_flutter: ^0.322.+1
Copy the code

The wrong sample

  • This is one of the worst ones. There are two callbacks in the widget, and only one of them will work
WebViewController _controller;
onWebViewCreated: (WebViewController wvc) {
	_controller = wvc;
}

onPageFinished: (String value) {
	_controller.evaluateJavascript( 'document.cookie = "SESSIONID=612bc4822b6996d6f335a963c20eb541fba72985; path=/"')}Copy the code
  • This one has only one cookie, which is fine. The difference is that this one uses double quotation marks to enclose single quotation marks, which is also painful to use
setSessionID() async {
  String sessionID = await LocalStorage.get("sessionID");
  if (Platform.isIOS) {
    _controller.evaluateJavascript("document.cookie = 'sessionID=${sessionID}'").then<String>((res) {
      print("webViewController.evaluateJavascript========>${res}");
      _onListCookies(_controller, context);
    });
  } else {
    _controller.evaluateJavascript('document.cookie = "sessionID=${sessionID};" ').then<String>((res) {
      print("webViewController.evaluateJavascript========>${res}"); _onListCookies(_controller, context); }); }}Copy the code

Add the correct writing method for multiple cookies

  • The cookie Settings need to be set after the page loads
///The webview controller
WebViewController _controller;
String _url = "Write your link";

WebView(
    initialUrl: _url,
    javascriptMode: JavascriptMode.unrestricted,
    onWebViewCreated: (controller) {
        _controller = controller;
    },
    onPageFinished: (url) {
        // The page has finished loading
		String cookie =
            "document.cookie = 'name=IAmDaShuaiBi'; document.cookie = 'id=233'";
        _controller.evaluateJavascript(cookie);
    },
    userAgent: "test; App / 1.0.0".)Copy the code
  • The most important change is that every cookie uses document.cookie as the key, which is the most critical

Optimize the writing

  • So the way I wrote it up here is to write it as a line, and writing it as a line is a deadly operation, and it makes assignment very confusing, so let’s optimize
///The webview controller
WebViewController _controller;
String _url = "Write your link";

WebView(
    initialUrl: _url,
    javascriptMode: JavascriptMode.unrestricted,
    onWebViewCreated: (controller) {
        _controller = controller;
    },
    onPageFinished: (url) {
        // The page has finished loading
        String cookie = ''' document.cookie = 'nameOne=IAmDaShuaiBi'; document.cookie = 'idOne=233'; document.cookie = 'nameTwo=IAmDaShuaiBi'; document.cookie = 'idTwo=233'; document.cookie = 'nameThree=IAmDaShuaiBi'; document.cookie = 'idThree=233'; ' ' ';
        _controller.evaluateJavascript(cookie);
    },
    userAgent: "test; App / 1.0.0".)Copy the code

The last

  • Ok, get