0

I've read an example on how to do this in android, and an unanswered question for android too, but does anyone know how to specify a folder for the SharedPreferences data to be stored in?

In my case, a device receives data from multiple devices, so I'd like to store that data under different folders (one folder per user). Currently they all sit in the default data/data/com.[]/shared_prefs.

If you want some code, this is my current SharedPreferences class:

import 'package:shared_preferences/shared_preferences.dart';

class MySharedPreferences {

  Future<Set<String>> getKeys() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    final value = prefs.getKeys();
    print("got ${value.length} results");
    return value;
  }

  Future getForm(String key) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    final value = prefs.getString(key) ?? 0;
    print('read: $value');
    return value;
  }

  deleteForm(String key) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.remove(key);
    print("deleted $key");
  }

  void saveForm(String json, String title) async{
    final prefs = await SharedPreferences.getInstance();
    final key = title;
    final value = json;
    prefs.setString(key, value);
    print('saved $value');
  }

  void saveName(String name) async{
    final prefs = await SharedPreferences.getInstance();
    final key = 'userName';
    final value = name;
    prefs.setString(key, value);
    print('saved $value');
  }
}

Usage:

MySharedPreferences().deleteForm(element);
afarre
  • 512
  • 6
  • 18

0 Answers0