How to load and query local json data in Flutter Mobile App?

3,076 views
Skip to first unread message

Niyazi Toros

unread,
Jul 16, 2018, 7:11:26 AM7/16/18
to Flutter Dev
Here is my key:value paired local json. I need to load and later I need to use for query.
So, how can I to load and query local json data in Flutter Mobile App?

{
  "currency.01": "United State USD",
  "currency.17": "British Pound GBP",
  "currency.33": "Euro EUR",
}

bre...@tamu.edu

unread,
Jul 16, 2018, 9:26:43 AM7/16/18
to Flutter Dev

I write settings to a file when I need this kind of functionality. For example, you can create a class for getting/setting file contents:


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;
 
}
}

You can then write json data to your settings file with something similar to:

final AppSettings settings = new AppSettings();


String jsonString = '{"testing": "true", "otherProperty": "other value"';
Future writeSettings = settings.writeUserSettings(jsonString);


And when you need to retrieve data from the settings file and parse as json:

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

Eugenio Tesio

unread,
Jul 16, 2018, 10:00:28 AM7/16/18
to bre...@tamu.edu, Flutter Dev
Maybe you can save the json in SharedPreference instead in a file. You must to import the shared preferences in pubspec.yaml

import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import 'dart:convert';
class AppSettings {
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));
}
}
It's just an idea, maybe will help you.

--
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.
--
Eugenio Tesio
Zetus - Soluciones Empresariales
División Sistemas
(+549) 3564-15-599945
San Francisco - 2400
Pcia de Córdoba - Argentina



Niyazi Toros

unread,
Jul 17, 2018, 12:30:56 AM7/17/18
to Flutter Dev
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. 

Eugenio Tesio

unread,
Jul 17, 2018, 7:08:02 AM7/17/18
to Niyazi Toros, Flutter Dev
This is in the documentation:

"Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing a persistent store for simple data. Data is persisted to disk asynchronously. Neither platform can guarantee that writes will be persisted to disk after returning and this plugin must not be used for storing critical data". 

I used this plugin and the values remain in the device always until you uninstall the app.

El mar., 17 jul. 2018 a las 1:30, Niyazi Toros (<niyazi...@gmail.com>) escribió:
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.

Niyazi Toros

unread,
Jul 17, 2018, 8:03:59 AM7/17/18
to Flutter Dev
Thanks @Eugenio Tesio.
Reply all
Reply to author
Forward
0 new messages