How to save to web local storage in flutter web

允我心安 提交于 2019-11-30 03:42:36

问题


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.


回答1:


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');
  }
}



回答2:


With flutter 1.10 we can use universal_html package:

import 'package:universal_html/prefer_universal/html.dart';
// ...
// read preference
var myPref = window.localStorage['mypref'];
// ...
// write preference
window.localStorage['mypref'] = myPref;




回答3:


After upgrading to flutter 1.9, 'dart:html' is not compiled anymore as it is not part of dart SDK that shipped with Flutter. We can use this package at the moment as it support Android, IOS and WEB: crypted_preferences



来源:https://stackoverflow.com/questions/56417667/how-to-save-to-web-local-storage-in-flutter-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!