Flutter入门之mac开发环境搭建

flutter

Flutter是Google从 I/O 2017开始着重推广的移动端跨平台开发框架。近年来出现了很多基于Javascript语言的优秀的移动端跨平台框架,它们各有千秋,其中Flutter越来越受到开发者的关注。它主要有以下几个突出的特点:

  1. Flutter从v0.2.4版本开始默认采用Dart 2。Dart 2是完整的静态强类型语言,它的语法对于Javascript和Java程序员来说比较好上手;
  2. Flutter引擎自带了大部分原生组件,大多数情况下不需要类似JS bridge的东西去跟原生组件通信;
  3. 配置开发环境的时候有一个flutter doctor的命令行工具,它可以检测到开发Flutter app时所需要的工具中哪些已经在系统里安装,哪些还没有,会给出详细友好的操作提示,基本上把它给出的命令复制黏贴在命令行里运行就可以了。Flutter官方也提供了非常详细的文档
  4. Flutter自带的组件非常丰富,使你不需要依赖第三方组件库就可以开发移动端app了;
  5. Flutter在单元测试、部件测试(widget testing)和集成测试等方面提供了丰富的测试工具以及详细的文档和用例
  6. Flutter有着强大而友好的命令行交互工具,是它对DevOps和CI/CD的支持非常友好;

看到Flutter这么多特性,我真的心动了。在mac本上配置了开发环境,启动了自己的第一个Flutter app。

下载Flutter SDK

由于众所周知的原因,我们需要用国内的下载源:
https://storage.flutter-io.cn/flutter_infra/releases/stable/macos/flutter_macos_v1.5.4-hotfix.2-stable.zip
下载完成后,将压缩包解压到方便你后续配置环境变量的目录下,我直接解压在了用户根目录下。然后修改.zshrc文件,加入一行:

export PATH=/Users/wubin1989/flutter/bin:$PATH

然后执行命令:

➜  ~ source ~/.zshrc
➜  ~ flutter --version
Flutter 1.5.4-hotfix.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 7a4c33425d (6 weeks ago) • 2019-04-29 11:05:24 -0700
Engine • revision 52c7a1e849
Tools • Dart 2.3.0 (build 2.3.0-dev.0.5 a1668566e5)

如果出现版本信息,就说明环境变量配好了。

执行flutter doctor

执行命令:

➜  ~ flutter doctor -v
[✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.3 18D109, locale zh-Hans-CN)
    • Flutter version 1.5.4-hotfix.2 at /Users/wubin1989/flutter
    • Framework revision 7a4c33425d (6 weeks ago), 2019-04-29 11:05:24 -0700
    • Engine revision 52c7a1e849
    • Dart version 2.3.0 (build 2.3.0-dev.0.5 a1668566e5)

 
[!] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /Users/wubin1989/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses

[!] iOS toolchain - develop for iOS devices (Xcode 10.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.2, Build version 10E125
    ✗ libimobiledevice and ideviceinstaller are not installed. To install with Brew, run:
        brew update
        brew install --HEAD usbmuxd
        brew link usbmuxd
        brew install --HEAD libimobiledevice
        brew install ideviceinstaller
    ✗ ios-deploy not installed. To install:
        brew install ios-deploy
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on
        the Dart side.
        Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
        For more info, see https://flutter.dev/platform-plugins
      To install:
        brew install cocoapods
        pod setup

[!] Android Studio (version 3.4)
    • Android Studio at /Applications/Android Studio.app/Contents
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)

[!] IntelliJ IDEA Ultimate Edition (version 2018.3.5)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • For information about installing plugins, see
      https://flutter.dev/intellij-setup/#installing-the-plugins

[!] VS Code (version 1.35.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    ✗ Flutter extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[!] Connected device
    ! No devices available

! Doctor found issues in 6 categories.

Doctor把检测到的问题和解决办法都提示出来了,一步步照着做即可。

配置IOS开发环境

首先是安装xcode,从apple store里就能下载安装,就不赘述了;
然后执行命令:

➜  ~ sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
➜  ~ sudo xcodebuild -license

打开模拟器:

➜  ~ open -a Simulator

创建和运行demo app:

➜  ~ flutter create newbie
Creating project newbie...
  newbie/ios/Runner.xcworkspace/contents.xcworkspacedata (created)
  newbie/ios/Runner/Info.plist (created)
  newbie/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png (created)
  newbie/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png (created)
  newbie/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md (created)
  newbie/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json (created)
  newbie/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png (created)
  newbie/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png (created)
  newbie/ios/Runner/Base.lproj/LaunchScreen.storyboard (created)
  newbie/ios/Runner/Base.lproj/Main.storyboard (created)
  newbie/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata (created)
  newbie/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme (created)
  newbie/ios/Flutter/Debug.xcconfig (created)
  newbie/ios/Flutter/Release.xcconfig (created)
  newbie/ios/Flutter/AppFrameworkInfo.plist (created)
  newbie/test/widget_test.dart (created)
  newbie/newbie.iml (created)
  newbie/.gitignore (created)
  newbie/.metadata (created)
  newbie/ios/Runner/AppDelegate.h (created)
  newbie/ios/Runner/main.m (created)
  newbie/ios/Runner/AppDelegate.m (created)
  newbie/ios/Runner.xcodeproj/project.pbxproj (created)
  newbie/android/app/src/profile/AndroidManifest.xml (created)
  newbie/android/app/src/main/res/mipmap-mdpi/ic_launcher.png (created)
  newbie/android/app/src/main/res/mipmap-hdpi/ic_launcher.png (created)
  newbie/android/app/src/main/res/drawable/launch_background.xml (created)
  newbie/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png (created)
  newbie/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png (created)
  newbie/android/app/src/main/res/values/styles.xml (created)
  newbie/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png (created)
  newbie/android/app/src/main/AndroidManifest.xml (created)
  newbie/android/app/src/debug/AndroidManifest.xml (created)
  newbie/android/gradle/wrapper/gradle-wrapper.properties (created)
  newbie/android/gradle.properties (created)
  newbie/android/settings.gradle (created)
  newbie/pubspec.yaml (created)
  newbie/README.md (created)
  newbie/lib/main.dart (created)
  newbie/android/app/build.gradle (created)
  newbie/android/app/src/main/java/com/example/newbie/MainActivity.java (created)
  newbie/android/build.gradle (created)
  newbie/android/newbie_android.iml (created)
  newbie/.idea/runConfigurations/main_dart.xml (created)
  newbie/.idea/libraries/Flutter_for_Android.xml (created)
  newbie/.idea/libraries/Dart_SDK.xml (created)
  newbie/.idea/libraries/KotlinJavaRuntime.xml (created)
  newbie/.idea/modules.xml (created)
  newbie/.idea/workspace.xml (created)
Running "flutter packages get" in newbie...                        20.6s
Wrote 66 files.

All done!
[✓] Flutter is fully installed. (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.3 18D109, locale
    zh-Hans-CN)
[✓] Android toolchain - develop for Android devices is fully installed. (Android SDK version 28.0.3)
[✓] iOS toolchain - develop for iOS devices is fully installed. (Xcode 10.2)
[!] Android Studio is partially installed; more components are available. (version 3.4)
[✓] IntelliJ IDEA Ultimate Edition is fully installed. (version 2018.3.5)
[!] VS Code is partially installed; more components are available. (version 1.35.0)
[✓] Connected device is fully installed. (1 available)

Run "flutter doctor" for information about installing additional components.

In order to run your application, type:

  $ cd newbie
  $ flutter run

Your application code is in newbie/lib/main.dart.

这里要提一下,下载依赖项的过程需要梯子,如果没有好用的梯子,建议配置mirror,修改.zshrc文件,添加下面两行:

 export PUB_HOSTED_URL=https://pub.flutter-io.cn
 export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

然后执行命令使修改生效:

➜  ~ source ~/.zshrc

再执行如下命令,app就在模拟器里启动了:

➜  ~ cd newbie
➜  newbie flutter run
Launching lib/main.dart on iPhone Xʀ in debug mode...
Running Xcode build...                                                  
 ├─Assembling Flutter resources...                           1.5s
 └─Compiling, linking and signing...                         1.9s
Xcode build done.                                            4.9s
Syncing files to device iPhone Xʀ...                                    
 2,010ms (!)                                       

🔥  To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
An Observatory debugger and profiler on iPhone Xʀ is available at:
http://127.0.0.1:50333/gAX4Ldz9lgA=/
For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".
app截图

部署到IOS设备

  1. 如果已经根据Doctor的提示把IOS toolchain里的问题清除了,就可以跳过下面这段配置:
➜  ~ brew update
➜  ~ brew install --HEAD usbmuxd
➜  ~ brew link usbmuxd
➜  ~ brew install --HEAD libimobiledevice
➜  ~ brew install ideviceinstaller ios-deploy cocoapods
➜  ~ pod setup

在项目根目录执行,打开xcode:

➜  newbie open ios/Runner.xcworkspace
  1. 添加账号


    xcode截图

    根据截图里的提示添加账号,需要登录apple id。

  2. 通过数据线连接你的iphone,并且在手机弹出的”是否信任这台电脑“的提示框里点击信任
  3. 生成签名(这一步可能会踩坑,有问题可以留言)


    xcode截图
  4. 执行命令:
newbie flutter run
Launching lib/main.dart on iPhone in debug mode...
Automatically signing iOS for device deployment using specified development team in Xcode project:
FF3R5PWHLT
Running Xcode build...                                                  
 ├─Assembling Flutter resources...                           1.0s
 └─Compiling, linking and signing...                         1.8s
Xcode build done.                                            4.4s
Installing and launching...                                         7.9s
Could not install build/ios/iphoneos/Runner.app on 1d38803c5b7e94c956b36641ca99a80fa51e3e96.
Try launching Xcode and selecting "Product > Run" to fix the problem:
  open ios/Runner.xcworkspace

Error launching application on iPhone.

如果出现报错,别担心,你需要在手机上做几步操作:
打开设置 => 点击通用 => 点击描述文件与设备管理 => 点击最下面的”开发者应用" => 点击”信任XXXX“


手机截图

再次执行命令:

➜  newbie flutter run
Launching lib/main.dart on iPhone in debug mode...
Automatically signing iOS for device deployment using specified development team in Xcode project:
FF3R5PWHLT
Running Xcode build...                                                  
 ├─Assembling Flutter resources...                           1.3s
 └─Compiling, linking and signing...                         2.9s
Xcode build done.                                            5.6s
Installing and launching...                                        11.8s
Syncing files to device iPhone...                                1,883ms

🔥  To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
An Observatory debugger and profiler on iPhone is available at: http://127.0.0.1:1025/XklrNtNqj_o=/
For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".

成功!

结论

亲自体验了一下Flutter的开发环境配置,app的创建和部署,感觉Flutter的命令行交互工具确实很棒,特别是flutter doctor,可以帮助开发者清除很多障碍,节省宝贵的时间。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,311评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,339评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,671评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,252评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,253评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,031评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,340评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,973评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,466评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,937评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,039评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,701评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,254评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,259评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,497评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,786评论 2 345

推荐阅读更多精彩内容

  • Flutter简介 Flutter 是 Google推出并开源的移动应用开发框架,主打跨平台、高保真、高性能。开发...
    moxuyou阅读 3,254评论 0 6
  • 为大家倾力打造的课程《Flutter从入门到进阶-实战携程网App》上线了,解锁Flutter开发新姿势,一网打尽...
    CrazyCodeBoy阅读 2,813评论 4 2
  • 1. 简介 Flutter是谷歌的移动UI框架,可以快速在iOS和Android上构建高质量的原生用户界面。 Fl...
    yangsg阅读 2,211评论 3 8
  • 真实故事计划100个 2018年的七夕节,我是从噩梦中惊醒哭着来到这一天的。还是那个可怕的梦,尽管现实中我强迫自己...
    麻辣小公子阅读 260评论 0 0
  • 徐先生的手机彻底坏了,或者严格的讲,离彻底坏不远了。 少了和徐先生的睡前聊天,一下子变得无聊乏味了许多。 所以今天...
    樹里阅读 109评论 0 0