How to save to web local storage in flutter web

后端 未结 6 1995
渐次进展
渐次进展 2020-12-30 01:02

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 ar

6条回答
  •  一向
    一向 (楼主)
    2020-12-30 01:50

    shared_preferences dart package now supports local storage for the web from version 0.5.4.7+

    Similar to shared preference on Android and iOS, the following is the code snippet for local storage on web

    import 'package:flutter/material.dart';
    import 'package:shared_preferences/shared_preferences.dart'; // rememeber to import shared_preferences: ^0.5.4+8
    
    
    void main() {
      runApp(MaterialApp(
        home: Scaffold(
          body: Center(
          child: RaisedButton(
            onPressed: _incrementCounter,
            child: Text('Increment Counter'),
            ),
          ),
        ),
      ));
    }
    
    _incrementCounter() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      int counter = (prefs.getInt('counter') ?? 0) + 1;
      print('Pressed $counter times.');
      await prefs.setInt('counter', counter);
    }
    

提交回复
热议问题