16

I am building a web application with flutter for web, i know for sure that in the mobile version of flutter there is a way to store data in the device, but i don't know if this was even implemented in flutter for web yet.

  • 2
    Possible duplicate of [How to save to web local storage in flutter web](https://stackoverflow.com/questions/56417667/how-to-save-to-web-local-storage-in-flutter-web) – Dariusz Bacinski Aug 14 '19 at 08:46

4 Answers4

6

You can use the SharedPreferences plugin for storing and retrieving persistent simple data. They gave support for the web from version 0.5.4+7

Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70
3

Apparently you can use localStorage: https://github.com/flutter/flutter/issues/35116

Gazihan Alankus
  • 11,256
  • 7
  • 46
  • 57
  • 2
    Is this secure to store data like tokens to maintain the user logged in my application? –  Jul 31 '19 at 09:22
  • 4
    I don't think anything in the client side is secure. I think saving tokens should be fine. – Gazihan Alankus Jul 31 '19 at 09:30
  • In case someone comes here; It seems by answers here https://stackoverflow.com/questions/56417667/how-to-save-to-web-local-storage-in-flutter-web this no longer works – user3808307 Oct 30 '20 at 03:50
2

Moor is a reactive persistence data storage library which is built on top of sqlite and is based on indexedDb. This library is compatible with most of web browsers.
You can use the package and see the documentation and example from official link.

Mohsen Emami
  • 2,709
  • 3
  • 33
  • 40
  • It seems that Moor is nowadays changed it's name to Drift. New link: https://pub.dev/packages/drift – Lemonius Apr 19 '23 at 06:06
1

You can use local storage from dart:html package but if your app also runs on mobile it is better to use universal_html package which provide all the features of dart:html.

If your app has mobile support, dart compiler will yell at you with this at the time Im writing this answer,

Avoid using web libraries, dart:html, dart:js and dart:js_util in Flutter packages that are not web plugins. These libraries are not supported outside a web context; functionality that depends on them will fail at runtime in Flutter mobile, and their use is generally discouraged in Flutter web.

Simple example with 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']);
}
Binozo
  • 145
  • 2
  • 10
Blasanka
  • 21,001
  • 12
  • 102
  • 104