Flutter development practice-Release environment and Debug debugging environment configuration implementation

Flutter development practice-Release environment and Debug debugging environment configuration implementation

In development, we often encounter configuration isolation such as Release and Debug related requests. The previous implementation is recorded here.
Before implementing, we need to look at git_it

一、get_it

During the development process, part of the logic of the App needs to be separated from the Widget. Both git_it and provider can be used for state management. Here, git_it is used. The reason why I use git_it is that it does not force to rely on context when obtaining.

GetIt is a simple service locator for Dart and Flutter projects with some extras inspired by Splat. It can be used in place of InheritedWidget or Provider eg to access objects from your UI.

1.1 Introducing git_it

pubspec.yaml introduces git_it

get_it: ^7.6.0

1.2 Using git_it

GetIt getIt = GetIt.instance;
// 或者
GetIt getIt = GetIt.I;

//重新自定义一个新的
GetIt getIt = GetIt.asNewInstance();

register

GetIt gitIt = GetIt.instance;

//在GetIt里注册工厂类TokenFetcher
gitIt.registerFactory<TokenFetcher>(() => TokenFetcherImpl());

call get

var tokenFetcher = gitIt.get<TokenFetcher>();

Here is a brief introduction to the usage. Release and Debug related requests need to use git_it.

Two, realize Config

To distinguish between Release and Debug related request configurations, we need to implement a CongfigManager

/// 定义config配置的key常量
class ConfigConstant {
    
    
  static String kAppHost = "appHost";
}

// 定义枚举,网络请求环境,DEV、PRE、PRO
enum ConfigEnv {
    
    
  dev, // Dev服务器环境
  pre, // Pre服务器环境 预发布环境
  pro, // Pre服务器环境 线上生产环境
}

/// 管理网络请求相关服务
abstract class ConfigManager extends ChangeNotifier {
    
    
  void init();

  String get(String key);

  int getImAppId();
}

2.1, Realize the configuration of Release ReleaseConfigMgr

Implement the ConfigManager of the Release environment

class ReleaseConfigMgr extends ConfigManager {
    
    
  ReleaseConfigMgr() {
    
    
    init();
  }

  
  String get(String key) {
    
    
    // 根据key获取请求的服务地址
    // TODO: implement get
    if (ConfigConstant. kAppHost == key) {
    
    
      return "https://app-s.ifour.cn/";
    }

    return "";
  }

  
  void init() {
    
    
    // TODO: implement init
  }

  
  int getImAppId() {
    
    
    // TODO: implement getImAppId
    return 111111111;
  }
}

2.2. Implement Debug Configuration DebugConfigMgr

Implement Debug Configuration DebugConfigMgr

class DebugConfigMgr extends ConfigManager {
    
    
  ConfigEnv _env = ConfigEnv.dev;

  DebugConfigMgr() {
    
    
    init();
  }

  
  String get(String key) {
    
    
    // TODO: implement get
    if (ConfigEnv.dev == _env) {
    
    
      if (ConfigConstant. kAppHost == key) {
    
    
         return "https://app-dev.ifour.cn/";
      }
    } else if (ConfigEnv.pre == _env) {
    
    
      if (ConfigConstant. kAppHost == key) {
    
    
         return "https://app-pre.ifour.cn/";
      }
    } else if (ConfigEnv.pro == _env) {
    
    
      if (ConfigConstant. kAppHost == key) {
    
    
         return "https://app-s.ifour.cn/";
      }
    }
    return "";
  }

  
  int getImAppId() {
    
    
    // TODO: implement getImAppId
    // 从本地取出相应的保存环境类型
    if (ConfigEnv.dev == _env) {
    
    
      /// IM sdk appId  Dev环境
      return 111111111;
    } else if (ConfigEnv.pre == _env) {
    
    
      /// IM sdk appId  Pre环境
      return 111111112;
    } else if (ConfigEnv.pro == _env) {
    
    
      /// IM sdk appId  Pro环境
      return 111111113;
    }
    return 0;
  }

  
  void init() {
    
    
    // TODO: implement init
    // 初始化的时候从本地取出
  }

  // 设置环境
  setEnv(ConfigEnv env) {
    
    
    // TODO: implement setEnv
    _env = env;
    // 更新本地存储的环境类型
    notifyListeners();
  }
}

3. Use get_it to set the config corresponding to Release and debug

Use get_it to set the config corresponding to Release and debug.
Since our debug and release configurations are different, I am using main.dart and main_debug.dart to represent different entries

3.1, configure in main.dart

// release

Future<void> main() async {
    
    
  WidgetsFlutterBinding.ensureInitialized();

  Global.init().then((e) {
    
    
    OnePlatform.app = () => const MyApp();
  });

  // 设置请求地址及IM
  GetIt.instance.registerSingleton<ConfigManager>(ReleaseConfigMgr(),
      signalsReady: true);

  // 初始化IM配置
  IMManager().configIM(imSDKAppId: GetIt.instance<ConfigManager>().getImAppId());
}

3.2, configure in main_debug.dart

// debug

Future<void> main() async {
    
    
  WidgetsFlutterBinding.ensureInitialized();

  Global.init().then((e) {
    
    
    OnePlatform.app = () => const MyApp();
  });

  // 设置请求地址及IM
  GetIt.instance.registerSingleton<ConfigManager>(debugConfigMgr,
      signalsReady: true);

  // 初始化IM配置
  IMManager().configIM(imSDKAppId: GetIt.instance<ConfigManager>().getImAppId());
}

3.3, the specific use in the code to obtain the request address

To get the request address specifically in the code, you can directly use GetIt.instance().get(ConfigConstant.kAppHost) to get the request server address

String requestUrl = GetIt.instance().get(ConfigConstant.kAppHost) + url;

specific request is available

static Future<ResponseData?> getAppInfo(String url, {
    
    required AppInfoReq req}) async {
    
    
    Map<String, dynamic>? params;
    if (req != null) {
    
    
      params = req.toJson();
    }
    String requestUrl = GetIt.instance<ConfigManager>().get(ConfigConstant.kAppHost) + url;
    return await SDHttp.getInstance().get(requestUrl, params: params);
  }

Four. Summary

Flutter development practice-Release environment and Debug debugging environment configuration implementation, use GetIt to configure, different environments can implement different config configurations, for example, the release request address is fixed, if it is debug, you can switch the development environment to handle more additional operate.

Learning records, keep improving every day.

Guess you like

Origin blog.csdn.net/gloryFlow/article/details/131642290