Flutter学习01:Mac环境搭建

张建 lol

Dart 介绍

  • Dart是由谷歌开发的计算机编程语言,它可以被用于 web、服务器、移动应用和物联网等 领域的开发

  • Dart诞生于2011年,号称要取代 JavaScript。但是过去的几年中一直不温不火。知道flutter的出现现在被人们重新重视

  • 要学flutter的话我们首先得会Dart

  • 官网:https://dart.dev/

Dart环境搭建

  • 要在我们本地开发 Dart 程序的话首先需要安装 Dart SDK

  • 官方文档:https://dart.dev/get-dart

  • Mac 安装:如果Mac电脑没有安装brew这个工具首先第一步需要安装它:https://brew.sh/

1
2
$ brew tap dart-lang/dart
$ brew install dart
  • 如果你安装了可以跳过此步骤
1
2
mac@bogon Blog % dart --version
Dart SDK version: 2.19.2 (stable) (Tue Feb 7 18:37:17 2023 +0000) on "macos_x64"

Dart 开发工具

  • Dart的开发工具有很多:Intellij IDEA、WebStream、Atom、Vscode等

  • 这里我们主要给大家讲解的是如何在Vscode中配置Dart

    • 找到 Vscode 插件安装 dart
    • 找到 Vscode 插件安装 code runnerCode Runner 可以运行我们的文件

下载Flutter

  1. 方式一:
1
2
mac@192 ~ % cd 目标文件路径
mac@192 ~ % unzip ~/Downloads/flutter_macos_3.3.10-stable.zip
  • 配置 flutterPATH 环境变量
1
mac@192 ~ % export PATH="$PATH:`pwd`/flutter/bin"

pwd:是 flutterbin 的文件路径

  1. 方式二:

直接从 Github 上下载:

1
mac@192 ~ % git clone https://github.com/flutter/flutter.git -b stable

环境变量设置

  • 进入根目录

mac@192 ~ % cd ~

  • 创建 .bash_profile

mac@192 ~ % touch .bash_profile

  • 编辑 .bash_profile

mac@192 ~ % open -e .bash_profile

  • 设置环境变量
1
2
3
4
# Flutter
export PUB_HOSTED_URL=https://pub.flutter-io.cn # 国内镜像
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn # 国内镜像
export PATH="$PATH:/Users/mac/Desktop/Flutter/flutter/bin"

注:export PATH="$PATH:【flutter的bin目录】

  • 保存 .bash_profile

mac@192 ~ % command+s

  • 更新

mac@192 ~ % source .bash_profile

  • 验证是否成功

mac@192 ~ % flutter doctor

如果终端显示如下,证明成功了,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Downloading Darwin x64 Dart SDK from Flutter engine 3316dd8728419ad3534e3f6112aa6291f587078a...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 202M 100 202M 0 0 5622k 0 0:00:36 0:00:36 --:--:-- 6859k
Building flutter tool...
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure
you trust this source!
Downloading Material fonts... 1,526ms
Downloading Gradle Wrapper... 262ms
Downloading package sky_engine... 473ms
Downloading flutter_patched_sdk tools... 1,162ms
Downloading flutter_patched_sdk_product tools... 638ms
Downloading darwin-x64 tools... 5.5s
Downloading libimobiledevice... 78ms
Downloading usbmuxd... 75ms
Downloading libplist... 75ms
Downloading openssl... 249ms
Downloading ios-deploy... 88ms
Downloading darwin-x64/font-subset tools... 255ms
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.10, on macOS 12.6.1 21G217 darwin-x64, locale
zh-Hans-CN)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
✗ cmdline-tools component is missing
Run `path/to/sdkmanager --install "cmdline-tools;latest"`
See https://developer.android.com/studio/command-line for more details.
✗ Android license status unknown.
Run `flutter doctor --android-licenses` to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/macos#android-setup for
more details.
[!] Xcode - develop for iOS and macOS (Xcode 14.1)
! CocoaPods 1.10.1 out of date (1.11.0 is recommended).
CocoaPods is used to retrieve the iOS and macOS platform side's plugin
code that responds to your plugin usage on the Dart side.
Without CocoaPods, plugins will not work on iOS or macOS.
For more info, see https://flutter.dev/platform-plugins
To upgrade see
https://guides.cocoapods.org/using/getting-started.html#installation for
instructions.
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] VS Code (version 1.73.1)
[✓] Connected device (3 available)
[✓] HTTP Host Availability
  • 针对 CocoaPods 1.10.1 out of date (1.11.0 is recommended) 我们只需要下载安装就可以了:

mac@192 ~ % sudo gem install -n /usr/local/bin cocoapods

通过 pod --version 查看最新版本

  • 针对 cmdline-tools component is missing 错误

打开 Android studiotools-->SDK manager 中进入 SDK tools 中:

找到 Android SDK 点击 Apply 进行下载

  • 再次 flutter doctor 运行,出现如下,则代表成功
1
2
3
4
5
6
7
8
9
10
11
12
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.10, on macOS 12.6.1 21G217 darwin-x64, locale
zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] VS Code (version 1.73.1)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

• No issues found!
  • 打开 Android studio 程序,安装 flutter:

  • 安装 dart

  • 安装完成,重新启动 Android studio,就可以开发 flutter

升级 flutter

  1. flutter SDK 所有分支

beat/dev/master/stable,其中stable为稳定分支,dev和master为开发分支,运行 flutter channel 查看所有分支

1
2
3
4
5
mac@bogon ~ % flutter channel
Flutter channels:
master
beta
* stable
  • “*” 号本地的 flutter SDK 跟踪分支,切换分支 flutter channel betaflutter channel master
  1. 升级 flutter sdk依赖包
1
flutter upgrade

该命令会同时更新 Flutter SDKflutter依赖包,如果只想更新依赖包:

1
2
flutter packages get 获取项目所有的依赖包
flutter packages upgrade 获取项目所有依赖包的最新版本

IDE配置和使用

Flutter 建议使用 Android StudioVS Code 之一开发体验,通过 IDE和插件 可获得代码补全、语法高亮、widget编辑辅助、运行和调试等功能,帮助我们极大的提高开发效率

  1. Android Studio

  2. VS Code

Flutter官网

连接到 ios 真机设备

Flutter官网

如果出现如下:

flutter真机运行 无法打开“iproxy”,因为无法验证开发者

执行以下命令:

1
2
3
4
sudo xattr -d com.apple.quarantine 
sudo xattr -d com.apple.quarantine
sudo xattr -d com.apple.quarantine
sudo xattr -d com.apple.quarantine /Users/mac/Desktop/Flutter/flutter/bin/cache/artifacts/usbmuxd/iproxy

其中 /Users/mac/Desktop/Flutter/flutter 是你桌面flutter的路径

  • Post title:Flutter学习01:Mac环境搭建
  • Post author:张建
  • Create time:2023-01-08 19:04:34
  • Post link:https://redefine.ohevan.com/2023/01/08/Flutter学习/Flutter学习01:Mac环境搭建/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.