import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
class AppSettings {
final String settingsFilename = "settings.json";
// get local path of documents directory
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
// get the settings file
Future<File> get _localFile async {
final path = await _localPath;
return new File('$path/$settingsFilename');
}
// write user setting to file
Future<File> writeUserSetting(String setting) async {
final file = await _localFile;
return file.writeAsString('$setting');
}
// read the file contents
Future<String> getUserSettings() async {
String contents;
try {
final file = await _localFile;
// read the file
contents = await file.readAsString();
} catch(e) {
print(e);
}
return contents;
}
Future<FileSystemEntity> removeUserSettingsFile() async {
final file = await _localFile;
return file.delete();
}
Future<bool> settingsFileExists() async {
File settingsFile = await _localFile;
if(await settingsFile.exists()) {
return true;
}
return false;
}
}
final AppSettings settings = new AppSettings();
String jsonString = '{"testing": "true", "otherProperty": "other value"';
Future writeSettings = settings.writeUserSettings(jsonString);
import 'dart:convert';
final AppSettings settings = new AppSettings();
Future getSettings = settings.getUserSettings();
getSettings.then((data) {
if(data != null) {
Map<String, dynamic> jsonData = json.decode(data);
print(jsonData['jsonKey']);
}
});import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import 'dart:convert';class AppSettings {It's just an idea, maybe will help you.
static Future<dynamic> getConcurrency() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return json.decode(prefs.getString('concurrency'));
}
static Future setConcurrency(var jsonConcurrency) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("concurrency", json.encode(jsonConcurrency));
}
}
--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thank you all. I am trying to explore 2 suggestions. The creating a get/set is a long process, not just for typing of course. The second suggestion given by Eugenio is much shorter decode I have a lot of local json files. So, @Eugenio Tesio what is the life span of SharedPreference? If you user puts the app in the background or exit from app, does the SharedPreference values dispose? I know I can save data in local mobile using SharedPreference but I am wondering a lifecycle.
--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.