Flutter For Web is there a way to store data?

6,411

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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.

Share:
6,411
Stecco
Author by

Stecco

Great and enthusiast programmer. Programming Languages known: Java(3 years of development) Python(2 years of development) C++(1 year of development) C#(3 years of development) JS(3 years of development) Frameworks known: NodeJS(2 years of development) JFoenix(1 year of development) D3.js(1 year of development) JxInput(1 year of development) Other: Android(2 years of development) Unity(3 years of development) MySQL(1 years of development) MongoDB(1 years of development) HTML(3 years of development) CSS(3 years of development) Languages Known: Italian(Mother Tongue) English Spanish

Updated on December 13, 2022

Comments

  • Stecco
    Stecco over 1 year

    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.