I am trying to load a config json file on the app startup, so I have acces to the configuration before the app is loaded.
My setup :
My code in the main.dart file :
import 'package:flutter/material.dart';
import 'dart:io';
import 'dart:convert';
void main(){
final file = new File('data/config1.json');
String content = file.readAsStringSync(encoding: Encoding.getByName("utf8"));
Map<String,dynamic> config = json.decode(content);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
...
}
But a receive an exception : FileSystemException: Cannot open file, path = 'data/config1.json' (OS Error: No such file or directory, errno = 2)
After that I would like to set the config from the json in a config singleton so I can have access to the configuration data everywhere in the app.
Any idea what i am doing wrong or how to achieve that ?
The code which is used to fetch data from the JSON file (see the full code below): Future<void> readJson() async { final String response = await rootBundle. loadString('assets/sample. json'); final data = await json.
Read from a fileOpen the file with openRead, which returns a stream that provides the data in the file as chunks of bytes. Read the stream to process the file contents when available. You can use various transformers in succession to manipulate the file content into the required format, or to prepare it for output.
++++ Update July 2019 ++++
There is a new Flutter package that uses the Flutter Global Configuration.
Github : https://github.com/Ephenodrom/EZ-Flutter
EZ Flutter supports managing different configuration files that can be accessed inside the app. The EzRunner loads different configuration files at startup.
From the Documentation :
The EzRunner loads automatically a json file with the name ez_settings.json from the assets directory.
The ez_settings.json should only contain configuration that refers to the EZ Framework.
EzSettingsKeys defines all settings available for the EZ Framework.
If envPath is set as an argument in the run method of the EzRunner, it loads the the configuration and it can be accessed via EzSettings env() method.
EzRunner.run(CustomWidget() ,
envPath: "assets/env_dev.json");
The environment .json file should contain configuration depending on the current environment the app is running.
The EzRunner will load the configurationf from the applicationPath and make it available via the app() method of the EzSettings.
EzRunner.run(CustomWidget(),
applicationPath: "assets/application.json");
The settings can be accessed via the EzSettings class.
Map<String, dynamic> ezSettings = EzSettings.ez();
Map<String, dynamic> envSettings = EzSettings.env();
Map<String, dynamic> appSettings = EzSettings.app();
++++ Old Answer ++++
It was simple as hell ... Just use the asynchronous way and set an "await" before runApp. So therefore it es easy to load the config file from the assets and have it ready before the app starts.
Future<String> loadFromAsset(String name) async {
String content = await rootBundle.loadString("assets/cfg/$name.json");
return content;
}
I wrote a simple flutter package for that problem. Check it out if your have the same situation. https://github.com/Ephenodrom/Flutter-Global-Config
import 'package:flutter/material.dart';
import 'package:global_configuration/global_configuration.dart';
void main() async{
GlobalConfiguration cfg = new GlobalConfiguration();
await cfg.loadFromAsset("app_settings").loadFromAsset("env_dev");
runApp(MyApp());
}
class MyApp extends StatelessWidget {
...
}
And then I can use the configuration anywhere I want.
import 'package:flutter/material.dart'; import 'package:global_configuration/global_configuration.dart';
class CustomWidget extends StatelessWidget {
CustomWiget(){
// Access the config in the constructor
GlobalConfiguration cfg = new GlobalConfiguration();
print(cfg.getAppConfig("key1"); // prints value1
}
@override
Widget build(BuildContext context) {
// Access the config in the build method
GlobalConfiguration cfg = new GlobalConfiguration();
return new Text(cfg.getAppConfig("key2"));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With