我正在使用Flutter for Web构建一个Web应用程序,我知道在Flutter的移动版本中,有一种方法可以将数据存储在设备中,但我不确定这是否已经在Flutter for Web中实现了。
显然,您可以使用 localStorage
:https://github.com/flutter/flutter/issues/35116
Moor
是一个基于 sqlite
并基于indexedDb 构建的响应式持久性数据存储库。该库兼容大多数Web浏览器。
您可以使用该软件包,并从官方链接查看文档和示例。
您可以使用dart:html
包中的本地存储,但如果您的应用程序也在移动设备上运行,则最好使用universal_html包,该包提供了dart:html
的所有功能。
如果您的应用程序具有移动支持,在我编写此答案时,dart编译器将向您发出以下警告:
避免在不是Web插件的Flutter包中使用Web库、dart:html、dart:js和dart:js_util。这些库不受Web环境之外的支持;依赖它们的功能将在Flutter移动端运行时失败,并且在Flutter Web中通常不鼓励使用它们。
使用universal_html的简单示例:
import 'package:universal_html/html.dart';
void main() {
String username = window.localStorage['username'];
if (username == null) {
window.localStorage['username'] = "dartyDev";
} else {
window.localStorage.remove('username');
}
// Get the latest updated value
print(window.localStorage['username']);
}