import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'in_app_webview_controller.dart'; import 'types.dart'; class WebStorage { LocalStorage localStorage; SessionStorage sessionStorage; WebStorage({@required this.localStorage, @required this.sessionStorage}); } class WebStorageItem { String key; dynamic value; WebStorageItem({this.key, this.value}); Map toMap() { return { "key": key, "value": value, }; } Map toJson() { return this.toMap(); } @override String toString() { return toMap().toString(); } } class Storage { InAppWebViewController _controller; WebStorageType webStorageType; Storage(InAppWebViewController controller, this.webStorageType) { assert(controller != null && this.webStorageType != null); this._controller = controller; } Future length() async { var result = await _controller.evaluateJavascript(source: """ window.$webStorageType.length; """); return result != null ? int.parse(json.decode(result)) : null; } Future setItem({@required String key, @required dynamic value}) async { var encodedValue = json.encode(value); await _controller.evaluateJavascript(source: """ window.$webStorageType.setItem("$key", ${value is String ? encodedValue : "JSON.stringify($encodedValue)"}); """); } Future getItem({@required String key}) async { var itemValue = await _controller.evaluateJavascript(source: """ window.$webStorageType.getItem("$key"); """); if (itemValue == null) { return null; } try { return json.decode(itemValue); } catch (e) {} return itemValue; } Future removeItem({@required String key}) async { await _controller.evaluateJavascript(source: """ window.$webStorageType.removeItem("$key"); """); } Future> getItems() async { var webStorageItems = []; List> items = (await _controller.evaluateJavascript(source: """ (function() { var webStorageItems = []; for(var i = 0; i < window.$webStorageType.length; i++){ var key = window.$webStorageType.key(i); webStorageItems.push( { key: key, value: window.$webStorageType.getItem(key) } ); } return webStorageItems; })(); """)).cast>(); if (items == null) { return webStorageItems; } for (var item in items) { webStorageItems .add(WebStorageItem(key: item["key"], value: item["value"])); } return webStorageItems; } Future clear() async { await _controller.evaluateJavascript(source: """ window.$webStorageType.clear(); """); } Future key({@required int index}) async { var result = await _controller.evaluateJavascript(source: """ window.$webStorageType.key($index); """); return result != null ? json.decode(result) : null; } } class LocalStorage extends Storage { LocalStorage(InAppWebViewController controller) : super(controller, WebStorageType.LOCAL_STORAGE); } class SessionStorage extends Storage { SessionStorage(InAppWebViewController controller) : super(controller, WebStorageType.SESSION_STORAGE); }