A simple trick, a hundred times faster Flutter development

In the development of Flutter, in order to achieve better decoupling and higher reuse, we will use modular ideas to deal with it. In Dart and Flutter, we will use Dart packages or plug-in packages, etc., and publish them to, for example, ourselves on the unpub server, and then aggregated and used in the shell project (main project).

For example, the yaml of a shell project is like this

 1 2 3 4 5 6 7 8 9 10 
dependencies : flutter : sdk : flutter firebase_crashlytics : 2.4.5 firebase_analytics : 9.0.5 basic : hosted : name : basic url : https://unpub.droidyue.com version : 1.6.2

So there is such a scenario, we want to add a method to the basic package and apply it to the main project.

But when performing processing, we will have such considerations

  • If the modified content is verified through unpub, the time cost is very large.
  • However, when publishing to unpub, certain quality control is required. The modified content must be verified before it can be published on the unpub server.

Therefore, we need to try to find a way to verify the modified content more quickly without going through unpub.

Fortunately, dart provides a dependency_overrides configuration item to deal with dependency overriding.

It is also very simple to use. The following is an example of our implementation of rewriting basic. Basically, we only need to care about the package name (basic) and path (../basic).

 1 2 3 
dependency_overrides : basic : path : "../basic"

Although the above method is provided to implement dependency rewriting, it is still not fast enough, because the usual scenario is like this

  • We usually use Android Studio to open the basic project, and AS has a lot of meeting cards.
  • When we want to adjust basic using dependency_overrides , we need to open the shell project again with Android studio. Of course, you can also use a text editor, but its support for yaml is not necessarily as good as AS, and it cannot be intelligently prompted.

So is there a faster way? The answer is yes, because yaml is structured, we can use scripts to generate the content corresponding to dependency_overrides .

The following is a ruby ​​script that implements the addition processing of yaml content.

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four 25 26 27 28 
#!/usr/bin/env ruby # encoding: utf-8 require 'yaml' file_path = ARGV [ 0 ] repo_name = ARGV [ 1 ] repo_path = ARGV [ 2 ] yaml_string = File . read file_path data = YAML . load yaml_string if data . key? ( 'dependency_overrides' ) hash = { repo_name . strip => { "path" => repo_path . strip } } data [ 'dependency_overrides' ] = data [ 'dependency_overrides' ]. merge ( hash ) else data [ 'dependency_overrides' ] = { repo_name . strip => { "path" => repo_path . strip } } end output = YAML . dump data File . write ( file_path , output )
  • Save the above script and name it localDartDep.rb,
  • Put its path into the environment variable PATH, then update the current bash
  • Switch to the current project path and execute localDartDep.rb basic ../basic to quickly complete the replacement operation. No need to open Android Studio, lightweight and fast.

For the above operations, it can still be packaged into a shell script for more convenient processing

 1 2 
#!/bin/bash localDartDep.rb "$1/pubspec.yaml" "basic" "../basic"

Then switch to the corresponding project and execute basicLocalDep.sh ./ .

droidyue_gzh_green_png.png

This article is reprinted from: https://droidyue.com/blog/2022/04/18/how-to-be-100x-faster-about-replace-dart-flutter-deps-in-the-development/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment