Flutter read file before startup

8,332

Solution 1

++++ 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"));
     }
}

Solution 2

You should use it as a flutter asset, take a look at this.

Share:
8,332
Ephenodrom
Author by

Ephenodrom

Updated on December 07, 2022

Comments

  • Ephenodrom
    Ephenodrom over 1 year

    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 :

    • ios
    • android
    • lib
      • main.dart
    • data
      • config1.json

    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 ?

  • Golden Lion
    Golden Lion almost 3 years
    I am having problems running my globalconfiguration with http 0.13.0. pubspec is throwing errors.