Reputation: 1893
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 ?
Upvotes: 9
Views: 13116
Reputation: 84
You can use Flutter Global Configuration package to load configurations from file, according to the env.
sample code
import 'package:flutter/material.dart';
import 'package:global_configuration/global_configuration.dart';
import 'AppSettings.config.dart';
import 'DevSettings.config.dart';
void main() async {
GlobalConfiguration().loadFromMap(appSettings).loadFromMap(devSettings);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp() {
// Access configuration at constructor
GlobalConfiguration cfg = new GlobalConfiguration();
print("Key1 has value ${cfg.getString("key1")}");
print("Key2 has value ${GlobalConfiguration().getString("key2")}");
print("Key5 has value ${cfg.getString("key5")}, this should be null!");
}
@override
Widget build(BuildContext context) {
// Access configuration at build method
GlobalConfiguration cfg = new GlobalConfiguration();
print("Key3 has value ${cfg.getString("key3")}");
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('Welcome to Flutter'),
),
body: Center(
child: Text('Hello World'),
),
),
);
}
}
Upvotes: 1
Reputation: 1893
++++ 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"));
}
}
Upvotes: 4