user7790873
user7790873

Reputation:

Flutter For Web is there a way to store data?

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.

Upvotes: 19

Views: 13944

Answers (4)

Blasanka
Blasanka

Reputation: 22437

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

Upvotes: 1

Mohsen Emami
Mohsen Emami

Reputation: 3142

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.

Upvotes: 2

Vinoth
Vinoth

Reputation: 9734

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

Upvotes: 6

Gazihan Alankus
Gazihan Alankus

Reputation: 11984

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

Upvotes: 3

Related Questions