Git Product home page Git Product logo

flutter-switch-environment's Introduction

引言

APP开发中经常会碰到至少两个不同的环境: 一个测试环境,一个生产环境。 那如何在我们就测试开发后,不破坏现有的代码,切换到不同的环境呢? fluter官方并没有给出推荐的方案, 在寻找使用不同的方式,认为多个main_ enviroment.dart 是最优雅的办法。

实现方案

基本思路

在项目lib目录下创建不同的 main_enviroment.dart 文件, 来区分不同的环境, 而每个main_.dart 包含不同的初始化配置, 开始全局注册

目录结构

lib/main_com.dart  # 共有main_com文件
lib/main_dev.dart  # 测试环境
lib/main_release.dart  # 生产环境

定义配置类

定义好配置类, 一般可能是key,api_url这里我仅仅用最常用的base_url

enum BuildFlavor { dev, release }

class BuildEnvironment {
  final BuildFlavor flavor;
  final String apiBaseUrl;

  BuildEnvironment.dev({
    this.apiBaseUrl,
  }) : this.flavor = BuildFlavor.dev;

  BuildEnvironment.release({
    this.apiBaseUrl,
  }) : this.flavor = BuildFlavor.release;
}

初始化

初始化不同的环境配置, 把配置作为全局变量, 或依赖注入用getItgetx都行, 我比较喜欢用依赖注入,这样代码可测试,清晰。 demo里用了getx

  1. 测试环境 main_dev.dart

    // lib/main_dev.dart
    void main() async {
      final buildEnv = BuildEnvironment.dev(
        apiBaseUrl: 'https://domain.dev/api',
      );
      // 放到一个可以全局访问的地方
      Get.put(buildEnv);
      mainCom();
    }
  2. 生产环境 main_release.dart

    // lib/main_release.dart
    void main() async {
        final buildEnv = BuildEnvironment.release(
        apiBaseUrl: 'https://domain.release/api',
        );
        // 1. 放到一个可以全局访问的地方
        Get.put(buildEnv);
        mainCom();
    }
  3. 共有代码 main_com.dart

    // lib/main_com.dart
    void mainCom() {
      runApp(MyApp());
    }

运行

  1. 命令行 配置 flutter run lib/main_dev.dart

  2. Android stuio 配置 在配置文件指定运行main_ enviroment.dart 举个例子测试环境 lib/main_dev.dart

  3. Vistual studio 配置 在目录下.vscode/launch.json

    {
      "version": "1.0.0",
      "configurations": [
        {
          "program": "lib/main_dev.dart",
          "request": "launch",
          "type": "dart"
        },
        {
          "name": "release",
          "program": "lib/main_release.dart",
          "request": "launch",
          "type": "dart"
        }
      ]
    }

打包

指定想要的文件 flutter build -t lib/main_dev.dart

点赞、收藏、Star

附录

代码仓库

Github代码仓库

参考引用

how-do-i-build-different-versions-of-my-flutter-app-for-qa-dev-prod

flutter-switch-environment's People

Contributors

dot-wei avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.