Flutter read file before startup
++++ 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 :
EZ settings
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.
Environment settings
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.
Application settings
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");
Accessing the settings
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")); }}
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'), ), ), ); }}