How do I build different versions of my Flutter app for qa/dev/prod?

后端 未结 9 1614
清歌不尽
清歌不尽 2020-12-01 00:14

I am building a Flutter app, and I have variables with different values for different environments (QA, dev, prod, etc). What\'s a good way to organize my app so I can easil

9条回答
  •  天命终不由人
    2020-12-01 00:56

    Update July 2019 :

    I wrote a Package that integrates the Flutter Global Config.

    EZ Flutter is a collection of widgets, packages and many more usefull things, mixed up in little framework. The aim is to make standard features available from scratch.

    Github : https://github.com/Ephenodrom/EZ-Flutter

    dependencies:
      ez_flutter: ^0.2.0
    

    Check out the documentation how using different configurations works.

    https://github.com/Ephenodrom/EZ-Flutter/blob/master/documentation/APPLICATION_SETTINGS.md

    ++++ OLD ANSWER ++++

    Additional information :

    I had the same problem and used the solution suggested by Seth Ladd. Therefore I also needed different configuration for each app version (dev / prod ) and i don't want to write the configuration in the main_dev.dart or in the main_prod.dart file.

    I wrote a simple flutter package that deals with having seperated configuration files and load them at app startup. The configuration is then available at each line of code in your app.

    https://github.com/Ephenodrom/Flutter-Global-Config

    How to use it :

    Create a json file under assets/cfg/$file.json

    Add assets/cfg to your pubspec.yaml

    Loading different configuration files at app start :

    import 'package:flutter/material.dart';
    import 'package:global_configuration/global_configuration.dart';
    
    void main() async{
      await GlobalConfiguration().loadFromAsset("app_settings");
      await GlobalConfiguration().loadFromAsset("env_dev_settings");
      runApp(MyApp());
    }
    class MyApp extends StatelessWidget {
      ...
    }
    

    Using the configuration in your app :

    import 'package:flutter/material.dart';
    import 'package:global_configuration/global_configuration.dart';
    
    class CustomWidget extends StatelessWidget {
    
        CustomWiget(){
            // Access the config in the constructor
            print(GlobalConfiguration().getString("key1"); // prints value1
        }
    
        @override
         Widget build(BuildContext context) {
            // Access the config in the build method
            return new Text(GlobalConfiguration().getString("key2"));
         }
    }
    

提交回复
热议问题