The Setup of the Flutter development environment is simple and can be found in the official documentation:

Flutter Chinese: flutterChina. Club /setup- macOS…
Dev /docs/get-st…

I. Construction of Flutter environment

Start with a brief introduction to the setup of the Flutter development environment
  1. Download the latest version of the Zip installation package, such as storage.googleapis.com/flutter_inf…
  2. Decompress the package to a local directory, such as ~/dev_tools/flutter
  3. Add the FLUTTER tool to your PATH

export PATH=~/dev_tools/flutter:$PATHCopy the code

2. The directory structure of Flutter

2.1 Flutter directory

What is in the folder that Flutter decompresses

admindeMacBook-Pro:flutter szw$ ls -la total 224 drwxr-xr-x@ 27 szw staff 918 11 15 20:12 . drwxr-xr-x 7 szw staff 238 11 of 12 thus.. -rw-r--r--@ 1 szw staff 8196 11 12 16:39 .DS_Store -rw-r--r--@ 1 szw staff 26010 10 18 01:03 .cirrus.yml -rw-r--r--@ 1 szw staff 362 10 18 01:03 .codecov.yml drwxr-xr-x@ 15 szw staff 510 11 16 10:40 .git -rw-r--r--@ 1 szw staff 516 10 18 01:03 .gitattributes drwxr-xr-x@ 6 szw staff 204 10 18 01:03 .github -rw-r--r-- 1 szw staff 1757 11 15 15:13 .gitignore drwxr-xr-x@ 6 szw staff 204 10 18 01:08 .idea drwxr-xr-x@ 8 szw staff 272 11 12 16:38 .pub-cache -rw-r--r--@ 1 szw staff  1581 10 18 01:03 AUTHORS -rw-r--r--@ 1 szw staff 481 10 18 01:03 CODEOWNERS -rw-r--r--@ 1 szw staff 2625 10 18 01:03 CODE_OF_CONDUCT.md -rw-r--r--@ 1 szw staff 3815 10 18 01:03 CONTRIBUTING.md -rw-r--r--@ 1 szw staff 1520 10 18 01:03 LICENSE -rw-r--r--@ 1 szw staff 1107 10 18 01:03 PATENT_GRANT -rw-r--r--@ 1 szw staff 4297 10 18 01:03 README.md -rw-r--r--@ 1 szw staff 8745 10 18 01:03 analysis_options.yaml drwxr-xr-x@ 6 szw staff 204 11 14 10:52 bin -rw-r--r--@ 1  szw staff 607 10 18 01:03 dartdoc_options.yaml drwxr-xr-x@ 15 szw staff 510 10 18 01:03 dev drwxr-xr-x@ 13 szw staff 442 10 18 01:03 examples -rw-r--r--@ 1 szw staff 1741 10 18 01:03 flutter_console.bat -rw-r--r--@ 1 szw staff 296 10 18 01:03 flutter_root.iml drwxr-xr-x@ 12 szw staff 408 11 6 20:48 packages -rw-r--r-- 1 szw staff 20 11 15 21:34 versionCopy the code

Git repository! Git repository!

2.2 Flutter branch

Let’s look at its branches

admindeMacBook-Pro:flutter szw$ git branch
* stableCopy the code

It turns out we’re in the stable branch.
Let’s look at what remote branches are

admindeMacBook-Pro:flutter szw$ git branch -r origin/HEAD -> origin/master origin/Hixie-patch-1 origin/Hixie-patch-2 origin/Hixie-patch-3 origin/Hixie-patch-4 origin/beta origin/dev origin/generated_explicit_type origin/hot_restart_lowercase_r origin/master origin/potato620-patch-1 origin/refactor origin/revert-37962-show-search-app-bar-theme origin/revert-38861-use_frame_timings origin/revert-38922-text-docs origin/revert-39627-master origin/revert-40607-animationOverview origin/revert-40634-goldenTriageBenchmark origin/revert-40710-flutterGold origin/revert-40726-roll_branch origin/revert-41415-exposeimage origin/revert-41780-fix_build origin/revert-42236-add_rally_to_examples origin/revert-43576-web_compiler Elf Origin/STABLE Origin /v1.4.5-hotfixes Origin/V1.4.6-hotfixes Origin/v1.4.9 - hotfixes origin/v1.5.4 - hotfixes origin/v1.7.8 - hotfixes origin/v1.9.1 - hotfixesCopy the code

If you look at the current version, usually every release will have a tag attached, so let’s look at the last few tags

Admindemacbook-pro :flutter SZW $git describe --tags' git rev-list --tags --max-count=5 'v1.12.2 v1.12.1-23-g985DA8313 V1.12.1-22 - g001959941 v1.12.1-21 - g66821916d v1.12.1-20 - gc0af77bf8Copy the code

The current version is V1.12.2

3. The execution process of Flutter command

3.1 Path of the flutter command

admindeMacBook-Pro:flutter szw$ which flutter /Users/szw/dev_tools/flutter/bin/flutter

That’s the PATH we added to PATH earlier

3.2 Contents of the Flutter script

#! /usr/bin/env bash
PROG_NAME="$(path_uri "$(follow_links "$BASH_SOURCE")")"
BIN_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
export FLUTTER_ROOT="$(cd "${BIN_DIR}/.." ; pwd -P)"
FLUTTER_TOOLS_DIR="$FLUTTER_ROOT/packages/flutter_tools"
SNAPSHOT_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.snapshot"
STAMP_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.stamp"
SCRIPT_PATH="$FLUTTER_TOOLS_DIR/bin/flutter_tools.dart"
DART_SDK_PATH="$FLUTTER_ROOT/bin/cache/dart-sdk"
DART="$DART_SDK_PATH/bin/dart"
PUB="$DART_SDK_PATH/bin/pub"
(upgrade_flutter) 3< "$PROG_NAME""$DART" --packages="$FLUTTER_TOOLS_DIR/.packages" $FLUTTER_TOOL_ARGS "$SNAPSHOT_PATH""$@"Copy the code

This is the contents of the flutter/bin/flutter file (deleted). You can see that this is a script file.

The main process is as follows:
  • Check whether flutter_tool is upgraded (upgrade_flutter).
  • Run $FLUTTER_ROOT/bin/cache/flutter_tools.snapshot with dart and pass the parameters to it.
The.snapshot file is equivalent to the.jar file in Java. Flutter_tools. snapshot is generated in upgrade_flutter.
Upgrade_flutter flows as follows:



Dart dart dart dart dart dart dart dart dart dart dart dart dart dart dart
Snapshot was created to improve operational efficiency.

3.3 Commands in FLUTTER Tools

Flutter_tools is a dart package. Flutter_tools is a dart package. Flutter_tools is a dart package

import 'package:flutter_tools/executable.dart' as executable;
void main(List<String> args) {
  executable.main(args);
}
Copy the code
The main method in Flutter_tools /executable.dart is called.


The executable, the path of the dart for packages/flutter_tools/lib/executable. Dart

Future<void> main(List<String> args) async {
  final bool verbose = args.contains('-v') || args.contains('--verbose');
  final bool doctor = (args.isNotEmpty && args.first == 'doctor') ||
      (args.length == 2 && verbose && args.last == 'doctor');
  final bool help = args.contains('-h') || args.contains('--help') ||
      (args.isNotEmpty && args.first == 'help') || (args.length == 1 && verbose);
  final bool muteCommandLogging = help || doctor;
  final bool verboseHelp = help&& verbose; await runner.run(args, <FlutterCommand>[ AnalyzeCommand(verboseHelp: verboseHelp), AssembleCommand(), AttachCommand(verboseHelp: verboseHelp), BuildCommand(verboseHelp: verboseHelp), ChannelCommand(verboseHelp: verboseHelp), CleanCommand(), ConfigCommand(verboseHelp: verboseHelp), CreateCommand(), DaemonCommand(hidden: !verboseHelp), DevicesCommand(), DoctorCommand(verbose: verbose), DriveCommand(), EmulatorsCommand(), FormatCommand(), GenerateCommand(), IdeConfigCommand(hidden: !verboseHelp), InjectPluginsCommand(hidden: ! verboseHelp), InstallCommand(), LogsCommand(), MakeHostAppEditableCommand(), PackagesCommand(), PrecacheCommand(), RunCommand(verboseHelp: verboseHelp), ScreenshotCommand(), ShellCompletionCommand(), TestCommand(verboseHelp: verboseHelp), TrainingCommand(), UnpackCommand(), UpdatePackagesCommand(hidden: !verboseHelp), UpgradeCommand(), VersionCommand(), ], verbose: verbose, muteCommandLogging: muteCommandLogging, verboseHelp: verboseHelp, overrides: <Type, Generator>{ // The build runner instance is not supportedin google3 because
       // the build runner packages are not synced internally.
       CodeGenerator: () => const BuildRunner(),
       WebCompilationProxy: () => BuildRunnerWebCompilationProxy(),
       // The web runner is not supported internally because it depends
       // on dwds.
       WebRunnerFactory: () => DwdsWebRunnerFactory(),
     });
}Copy the code

Executable. Dart then transfers execution to the specific Command, based on the Command line arguments.

For example, when the flutter build command is run on the command line, it is ultimately handled by the BuildCommand class.
Other commands are similar. When we want to understand the principle of a flutter Command, we can look at the source code of the corresponding Command.

Four,

Some SDKS usually deliver compiled executables, while Flutter is a direct source repository. Build tools are generated directly from the source code. This can improve development efficiency and reduce maintenance costs for a project that is undergoing constant iteration and update.