如何在Flutter Web中保存到Web本地存储 [英] How to save to web local storage in flutter web

查看:1254
本文介绍了如何在Flutter Web中保存到Web本地存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 flutter for web 构建的网站,目前正在尝试保存到Web本地存储或cookie,但似乎找不到任何插件或归档方式。

I have a web site built with flutter for web and currently, am trying to save to web local storage or cookie but can't seem to find any plugin or way to archive that.

推荐答案

您可以从 dart使用 window.localStorage :html

You can use window.localStorage from dart:html

import 'dart:html';

class IdRepository {
  final Storage _localStorage = window.localStorage;

  Future save(String id) async {
    _localStorage['selected_id'] = id;
  }

  Future<String> getId() async => _localStorage['selected_id'];

  Future invalidate() async {
    _localStorage.remove('selected_id');
  }
}

这篇关于如何在Flutter Web中保存到Web本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆