Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter read file before startup

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 ?

like image 990
Ephenodrom Avatar asked Nov 25 '18 15:11

Ephenodrom


People also ask

How do I read a JSON file in Flutter?

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.

How do you read a dart file?

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.


1 Answers

++++ 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"));
     }
}
like image 176
Ephenodrom Avatar answered Sep 22 '22 10:09

Ephenodrom