Flutter's Webview - How to clear session status?
Asked Answered
D

3

14

I have an App that login to a specific page and execute some commands via Javascript. But when I start the App again it should open the login form. But, instead, it stays logged and goes to the user's screen.

        child: WebView(
              initialUrl:
                  'https://thepage.com/m/customer/account/login/',
              onWebViewCreated: (c) {
                _webviewController = c;
                print("cleaning the cache");
                _webviewController.clearCache();
              },
              onPageFinished: (String page) async {
                setState(() {
                  _isPageLoaded = true;
                });
              },
              javascriptMode: JavascriptMode.unrestricted,
            ),

I tried to clean the cache, but perhaps it is not enough.

I/zygote64( 5259): Compiler allocated 6MB to compile void android.view.ViewRootImpl.performTraversals()
I/flutter ( 5259): cleaning the cache
Draggletailed answered 13/7, 2019 at 17:54 Comment(1)
I don't think this currently works. See github.com/flutter/flutter/issues/53122Otic
B
20

I think the problem is that you're not clearing the cookies. I was having the same issue with logging in to a page, and it staying logged in, which I fixed by clearing the cookies in the onWebViewCreated callback:

onWebViewCreated: (webViewController) {
    _controller.complete(webViewController);
    webViewController.clearCache();
    final cookieManager = CookieManager();
    cookieManager.clearCookies();
},
Bona answered 19/6, 2020 at 16:23 Comment(1)
how can I clear cookies, while using InAppWebView?Fayola
T
3

You should clear cookies to start a new session, to clear cookies you can do the following `

late final WebViewCookieManager cookieManager = WebViewCookieManager();
 void clearCookies() async {
   await cookieManager.clearCookies();
 }

and call the function ininit`

Thetisa answered 13/2, 2023 at 2:58 Comment(0)
C
0

just use webController.clearLocalStorage();

Crease answered 7/7, 2024 at 7:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.