global_configuration 0.1.3

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new73

Flutter Global Configuration

A flutter package for managing different configurations by merging them together and making them available everythere inside the app via a singleton.

Table of Contents

  1. Install
  2. Import
  3. Loading configuration
  4. Using the configuration in your app
  5. Full Example
  6. Changelog
  7. Copyright and license

Install

pubspec.yaml

Update pubspec.yaml and add the following line to your dependencies.

dependencies:
  global_configuration: ^0.1.2

Import

Import the package with :

import 'package:global_configuration/global_configuration.dart';

Loading configuration

There are many ways where you can store your configuration files and load them.

Load from asset

Creating a configuration file

Create a .json file and place it under assets/cfg/ Example filename: assets/cfg/app_settings.json

{
  "key1": "value1",
  "key2": "value2"
}

Remember to update your pubspec.yaml file, so the config can be loaded from the assets directory! The config files must be placed under the assets/cfg/ directory.

flutter:
 assets:
  - assets/cfg/

Load configuration at app start

import 'package:flutter/material.dart';
import 'package:global_configuration/global_configuration.dart';

void main() async{
  await GlobalConfiguration().loadFromAsset("app_settings");
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  ...
}

Load from map inside .dart file

Creating a configuration file

Create a dart file which includes your configuration and import it in your main file. Example filename: /config/app_settings.config.dart

final Map<String, String> appSettings = {
  "key1": "value1",
  "key2": "value2"
};

Load configuration at app start

import 'package:flutter/material.dart';
import 'package:global_configuration/global_configuration.dart';
import 'config/Config1.config.dart';

void main(){
  GlobalConfiguration().loadFromMap(appSettings);
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  ...
}

Load from url

It is possible to load any json configuration file from a url. Use the method loadFromUrl to load the config via GET request. Please consider using a try / catch. This method will throw an exception if the status code of the GET request is not 200.

The method also accepts query parameters and headers! The header "Accept: application/json" is always included.

Load configuration at app start

import 'package:flutter/material.dart';
import 'package:global_configuration/global_configuration.dart';

void main() async{
  try{
    await GlobalConfiguration().loadFromUrl("http://urlToMyConfig.com");
  }catch(e){
    // something went wrong while fetching the config from the url ... do something
  }
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  ...
}

Using the configuration in your app

Instatiate the GlobalConfiguration class and call the getAppConfig($key) or getSharedConfig($key) method.

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.getString("key1"); // prints value1
    }

    @override
     Widget build(BuildContext context) {
        // Access the config in the build method
        GlobalConfiguration cfg = new GlobalConfiguration();
        return new Text(cfg.getString("key2"));
     }
}

Full example

You can find a full example in the example folder.

Changelog

For a detailed changelog, see the CHANGELOG.md file

MIT License

Copyright (c) 2018 Ephenodrom

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

[0.0.1] - 2018-11-25.

  • Initial release

[0.0.2] - 2018-11-25.

  • Fill README.md with content and examples

[0.1.0] - 2018-11-25.

  • Add example folder

[0.1.1] - 2018-11-25.

  • Add loading file from asset folder

example/main.dart

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);
  await GlobalConfiguration().loadFromUrl("https://swapi.co/api/people/1");
  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'),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  global_configuration: ^0.1.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:global_configuration/global_configuration.dart';
  
Version Uploaded Documentation Archive
0.1.3 Dec 9, 2018 Go to the documentation of global_configuration 0.1.3 Download global_configuration 0.1.3 archive
0.1.2 Nov 26, 2018 Go to the documentation of global_configuration 0.1.2 Download global_configuration 0.1.2 archive
0.1.1 Nov 25, 2018 Go to the documentation of global_configuration 0.1.1 Download global_configuration 0.1.1 archive
0.1.0 Nov 25, 2018 Go to the documentation of global_configuration 0.1.0 Download global_configuration 0.1.0 archive
0.0.2 Nov 25, 2018 Go to the documentation of global_configuration 0.0.2 Download global_configuration 0.0.2 archive
0.0.1 Nov 25, 2018 Go to the documentation of global_configuration 0.0.1 Download global_configuration 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
47
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
73
Learn more about scoring.

We analyzed this package on Dec 9, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7
  • Flutter: 1.0.0

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
http ^0.12.0 0.12.0
Transitive dependencies
async 2.0.8
charcode 1.1.2
collection 1.14.11
http_parser 3.1.3
meta 1.1.6
path 1.6.2
sky_engine 0.0.99
source_span 1.4.1
string_scanner 1.0.4
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test