macOS 上搭建Flutter開發環境

xnllc發表於2020-10-16


 

1. 環境

 

  1. 修改環境變數 修改檔案 ~/.bash_profile
1. 修改~/.bash_profile檔案
   檔案內容新增:
   export PUB_HOSTED_URL=https://pub.flutter-io.cn
   export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
2. 執行修改 source ~/.bash_profile
  1. 下載程式碼 git clone -b stable https://gitee.com/netscript/flutter
~/code/flutter ⌚ 11:08:17
$ git clone -b stable https://gitee.com/netscript/flutter
正克隆到 'flutter'...
remote: Enumerating objects: 234638, done.
remote: Counting objects: 100% (234638/234638), done.
remote: Compressing objects: 100% (48313/48313), done.
remote: Total 234638 (delta 180534), reused 232882 (delta 178778), pack-reused 0
接收物件中: 100% (234638/234638), 96.73 MiB | 427.00 KiB/s, 完成.
處理 delta 中: 100% (180534/180534), 完成.
正在檢出檔案: 100% (4207/4207), 完成.

~/code/flutter ⌚ 11:13:55
  1. 安裝 本來只是進入bin中 想檢視一下flutter版本號,也會先安裝
~/code/flutter/flutter/bin on  stable ⌚ 11:21:15
$ ./flutter -v
Downloading Dart SDK from Flutter engine af51afceb8886cc11e25047523c4e0c7e1f5d408...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  200M  100  200M    0     0   931k      0  0:03:40  0:03:40 --:--:-- 1067k
Building flutter tool...
  ╔════════════════════════════════════════════════════════════════════════════╗
  ║ A new version of Flutter is available!                                     ║
  ║                                                                            ║
  ║ To update to the latest version, run "flutter upgrade".                    ║
  ╚════════════════════════════════════════════════════════════════════════════╝


Manage your Flutter app development.

Common commands:

  flutter create <output directory>
    Create a new Flutter project in the specified directory.

  flutter run [options]
    Run your Flutter application on an attached device or in an emulator.

Usage: flutter <command> [arguments]

Global options:
-h, --help                     Print this usage information.
-v, --verbose                  Noisy logging, including all shell commands executed.
                               If used with --help, shows hidden options.

    --quiet                    Reduce the amount of output from some commands.
    --[no-]wrap                Toggles output word wrapping, regardless of whether or not the output is a
                               terminal.
                               (defaults to on)

    --wrap-column              Sets the output wrap column. If not set, uses the width of the terminal.
                               No wrapping occurs if not writing to a terminal. Use --no-wrap to turn off
                               wrapping when connected to a terminal.

-d, --device-id                Target device id or name (prefixes allowed).
    --version                  Reports the version of this tool.
    --machine                  When used with the --version flag, outputs the information using JSON.
    --[no-]color               Whether to use terminal colors (requires support for ANSI escape
                               sequences).
                               (defaults to on)

    --[no-]version-check       Allow Flutter to check for updates when this command runs.
                               (defaults to on)

    --suppress-analytics       Suppress analytics reporting when this command runs.
    --bug-report               Captures a bug report file to submit to the Flutter team.
                               Contains local paths, device identifiers, and log snippets.

    --packages                 Path to your ".packages" file.
                               (required, since the current directory does not contain a ".packages"
                               file)

    --flutter-root             The root directory of the Flutter repository.
                               Defaults to $FLUTTER_ROOT if set, otherwise uses the parent of the
                               directory that the "flutter" script itself is in.

Local build selection options (not normally required):
    --local-engine-src-path    Path to your engine src directory, if you are building Flutter locally.
                               Defaults to $FLUTTER_ENGINE if set, otherwise defaults to the path given
                               in your pubspec.yaml dependency_overrides for sky_engine, if any, or,
                               failing that, tries to guess at the location based on the value of the
                               --flutter-root option.

    --local-engine             Name of a build output within the engine out directory, if you are
                               building Flutter locally.
                               Use this to select a specific version of the engine if you have built
                               multiple engine targets.
                               This path is relative to --local-engine-src-path/out.

Options for testing the "flutter" tool itself:
    --record-to                Enables recording of process invocations (including stdout and stderr of
                               all such invocations), and file system access (reads and writes).
                               Serializes that recording to a directory with the path specified in this
                               flag. If the directory does not already exist, it will be created.

    --replay-from              Enables mocking of process invocations by replaying their stdout, stderr,
                               and exit code from the specified recording (obtained via --record-to). The
                               path specified in this flag must refer to a directory that holds
                               serialized process invocations structured according to the output of
                               --record-to.

    --show-test-device         List the special 'flutter-tester' device in device listings. This headless
                               device is used to
                               test Flutter tooling.

Available commands:
  analyze                  Analyze the project's Dart code.
  assemble                 Assemble and build flutter resources.
  attach                   Attach to a running application.
  bash-completion          Output command line shell completion setup scripts.
  build                    Flutter build commands.
  channel                  List or switch flutter channels.
  clean                    Delete the build/ and .dart_tool/ directories.
  config                   Configure Flutter settings.
  create                   Create a new Flutter project.
  daemon                   Run a persistent, JSON-RPC based server to communicate with devices.
  devices                  List all connected devices.
  doctor                   Show information about the installed tooling.
  drive                    Runs Flutter Driver tests for the current project.
  emulators                List, launch and create emulators.
  format                   Format one or more dart files.
  generate                 run code generators.
  help                     Display help information for flutter.
  ide-config               Configure the IDE for use in the Flutter tree.
  inject-plugins           Re-generates the GeneratedPluginRegistrants.
  install                  Install a Flutter app on an attached device.
  logs                     Show log output for running Flutter apps.
  make-host-app-editable   Moves host apps from generated directories to non-generated directories so
                           that they can be edited by developers.
  precache                 Populates the Flutter tool's cache of binary artifacts.
  pub                      Commands for managing Flutter packages.
  run                      Run your Flutter app on an attached device.
  screenshot               Take a screenshot from a connected device.
  test                     Run Flutter unit tests for the current project.
  update-packages          Update the packages inside the Flutter repo.
  upgrade                  Upgrade your copy of Flutter.
  version                  List or switch flutter versions.

Run "flutter help <command>" for more information about a command.
Run "flutter help -v" for verbose help output, including less commonly used options.

  ╔════════════════════════════════════════════════════════════════════════════╗
  ║                 Welcome to Flutter! - https://flutter.dev                  ║
  ║                                                                            ║
  ║ The Flutter tool uses Google Analytics to anonymously report feature usage ║
  ║ statistics and basic crash reports. This data is used to help improve      ║
  ║ Flutter tools over time.                                                   ║
  ║                                                                            ║
  ║ Flutter tool analytics are not sent on the very first run. To disable      ║
  ║ reporting, type 'flutter config --no-analytics'. To display the current    ║
  ║ setting, type 'flutter config'. If you opt out of analytics, an opt-out    ║
  ║ event will be sent, and then no further information will be sent by the    ║
  ║ Flutter tool.                                                              ║
  ║                                                                            ║
  ║ By downloading the Flutter SDK, you agree to the Google Terms of Service.  ║
  ║ Note: The Google Privacy Policy describes how data is handled in this      ║
  ║ service.                                                                   ║
  ║                                                                            ║
  ║ Moreover, Flutter includes the Dart SDK, which may send usage metrics and  ║
  ║ crash reports to Google.                                                   ║
  ║                                                                            ║
  ║ Read about data we send with crash reports:                                ║
  ║ https://github.com/flutter/flutter/wiki/Flutter-CLI-crash-reporting        ║
  ║                                                                            ║
  ║ See Google's privacy policy:                                               ║
  ║ https://www.google.com/intl/en/policies/privacy/                           ║
  ╚════════════════════════════════════════════════════════════════════════════╝



~/code/flutter/flutter/bin on  stable ⌚ 11:29:06
~/code/flutter/flutter/bin on  stable ⌚ 11:33:44
$ ./flutter upgrade
Upgrading Flutter from /Users/zj/code/flutter/flutter...
更新 cf37c2cd0..f139b1100
 50 files changed, 972 insertions(+), 470 deletions(-)
Flutter is already up to date on channel stable
Flutter 1.12.13+hotfix.9 • channel stable • https://gitee.com/netscript/flutter
Framework • revision f139b11009 (6 個月前) • 2020-03-30 13:57:30 -0700
Engine • revision af51afceb8
Tools • Dart 2.7.2
  1. 執行flutter doctor
~/code/flutter/flutter/bin on  stable ⌚ 11:34:25
$ ./flutter doctor
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Downloading android-arm-profile/darwin-x64 tools...                 2.2s
Downloading android-arm-release/darwin-x64 tools...                 3.8s
Downloading android-arm64-profile/darwin-x64 tools...               2.6s
Downloading android-arm64-release/darwin-x64 tools...               1.9s
Downloading android-x64-profile/darwin-x64 tools...                 2.9s
Downloading android-x64-release/darwin-x64 tools...                 1.6s
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.12.13+hotfix.9, on Mac OS X 10.15.6 19G2021, locale zh-Hans-CN)
[!] Android toolchain - develop for Android devices (Android SDK version 27.0.3)
    ✗ Flutter requires Android SDK 28 and the Android BuildTools 28.0.3
      To update using sdkmanager, run:
        "/usr/local/Cellar/android-sdk/24.4.1_1/tools/bin/sdkmanager" "platforms;android-28"
        "build-tools;28.0.3"
      or visit https://flutter.dev/setup/#android-setup for detailed instructions.
    ✗ Android license status unknown.
      Try re-installing or updating your Android SDK Manager.
      See https://developer.android.com/studio/#downloads or visit
      https://flutter.dev/setup/#android-setup for detailed instructions.

[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
[!] Android Studio (not installed)
[✓] VS Code (version 1.49.0)
[!] Connected device
    ! No devices available

! Doctor found issues in 3 categories.

~/code/flutter/flutter/bin on  stable ⌚ 11:36:22
  1. 新增環境變數 export PATH=/Users/zj/code/flutter/flutter/bin:$PATH

注意: 如果你使用的是zsh,終端啟動時 ~/.bash_profile 將不會被載入,解決辦法就是修改 ~/.zshrc ,在其中新增:source ~/.bash_profile

 

 

2. helloworld

2.1 ISO 模擬器執行

  1. 執行模擬器
open -a Simulator

 

image.png

image.png

出現上圖是可能是前面 flutter/bin 沒有放到PATH中(1.5中)

image.png

  1. 建立專案

image.png

image.png

  1. ios模擬器執行正常

image.png

第一次執行 看起來要六七分鐘

在終端裡面可以輸入的一些基本命令:

r : 熱載入; R : 熱重啟; h : 獲取幫助; d : 斷開連線; q : 退出;

image.png

2.2 Android 執行

 

 

 

 

參考:

https://flutterchina.club/setup-macos/ 社群

https://book.flutterchina.club/chapter2/first_flutter_app.html

https://www.cnblogs.com/mamamia/p/13652235.html 一個具體安裝文件參考

https://www.awaimai.com/2835.html 國內各種映象

相關文章