问题
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