[英]Local Storage property does not work in Flutter Webview Plug-in
我想在我的颤振应用程序的 WebView 的本地存储中设置一些键值对。 我正在使用名为flutter_webview_plugin的库。
我知道这个问题。
最终我想设置一个令牌,以便直接访问需要身份验证的 URL,该 URL 在 Chrome 的本地存储中存储为“jwt_token”。
我使用的库提供了一个withLocalStorage
属性和一个evalJavascript
方法:
flutterWebviewPlugin
.launch("SOME_URL",
withLocalStorage: true, withJavascript: true,)
.whenComplete(() {
flutterWebviewPlugin.evalJavascript("window.localStorage.setItem('key', 'key')");
flutterWebviewPlugin.evalJavascript("alert(window.localStorage.getItem('key'))");
flutterWebviewPlugin.evalJavascript("alert('test alert')");
运行上面的代码后,我的 webview 浏览器中会弹出“测试警报”,这表明evalJavascript
方法工作正常,但之前使用localStorage.getItem
方法的警报没有弹出。 我尝试过使用和不使用 window 对象,所有 '' "" 组合,结果都是一样的。 我无法使用此 JS 方法在本地存储中设置信息。 你能帮我吗 ?
你可以使用我的插件flutter_inappwebview ,这是一个 Flutter 插件,它允许你添加内联 WebView 或打开应用程序内浏览器窗口,并且有很多事件、方法和选项来控制 WebView。
默认情况下启用localStorage
功能!
这是一个在页面停止加载时设置和检索localStorage
值的快速示例:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
InAppWebViewController _webViewController;
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('InAppWebView Example'),
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: InAppWebView(
initialUrl: "https://github.com/flutter",
initialHeaders: {},
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
debuggingEnabled: true,
),
),
onWebViewCreated: (InAppWebViewController controller) {
_webViewController = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) async {
await controller.evaluateJavascript(source: "window.localStorage.setItem('key', 'localStorage value!')");
await controller.evaluateJavascript(source: "alert(window.localStorage.getItem('key'))");
},
))
])),
),
);
}
}
截屏:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.