Flutter hello_world

今天尝试写第一个Flutter程序,Flutter版的HelloWorld。

首先创建好存放工程的目录,然后使用以下Flutter命令创建工程:

flutter create helloWorld

我第一次执行该命令大约几分钟后才有反应,所以耐心等待,然后出来报错:

"helloWorld" is not a valid Dart package name.


From the [Pubspec format
description](https://www.dartlang.org/tools/pub/pubspec.html):

**DO** use `lowercase_with_underscores` for package names.

Package names should be all lowercase, with underscores to separate words,
`just_like_this`.  Use only basic Latin letters and Arabic digits: [a-z0-9_].
Also, make sure the name is a valid Dart identifier -- that it doesn't start
with digits and isn't a reserved word.

意思是命名不符合规范,"helloWorld"不是有效的Dart包名。Dart要求包名必须全部是小写字母,并使用下划线分割。包名和C语言变量名一样,只能使用字母和数字,并且不能使用数字开头、不能使用保留字。于是我们修改包名为"hello_world"重新创建工程:

flutter create hello_world

这次没有经过等待,立即就开始创建工程。从log中可以看到在hello_word目录下,分别创建了一个ios和android的文件夹,对应ios和android工程:

Creating project hello_world... androidx: true
  hello_world/ios/Runner.xcworkspace/contents.xcworkspacedata (created)
  hello_world/ios/Runner/Info.plist (created)
  hello_world/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png
  (created)
  hello_world/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png
  (created)
  hello_world/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md
  (created)
  hello_world/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json
  (created)
  hello_world/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png
  (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.pn
  g (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.pn
  g (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.pn
  g (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.pn
  g (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1
  x.png (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2
  x.png (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.pn
  g (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json
  (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.pn
  g (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.pn
  g (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.pn
  g (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.pn
  g (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.pn
  g (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.pn
  g (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.pn
  g (created)
  hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.pn
  g (created)
  hello_world/ios/Runner/Base.lproj/LaunchScreen.storyboard (created)
  hello_world/ios/Runner/Base.lproj/Main.storyboard (created)
  hello_world/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata
  (created)
  hello_world/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme
  (created)
  hello_world/ios/Flutter/Debug.xcconfig (created)
  hello_world/ios/Flutter/Release.xcconfig (created)
  hello_world/ios/Flutter/AppFrameworkInfo.plist (created)
  hello_world/ios/.gitignore (created)
  hello_world/test/widget_test.dart (created)
  hello_world/hello_world.iml (created)
  hello_world/.gitignore (created)
  hello_world/.metadata (created)
  hello_world/android/app/src/profile/AndroidManifest.xml (created)
  hello_world/android/app/src/main/res/mipmap-mdpi/ic_launcher.png (created)
  hello_world/android/app/src/main/res/mipmap-hdpi/ic_launcher.png (created)
  hello_world/android/app/src/main/res/drawable/launch_background.xml (created)
  hello_world/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png (created)
  hello_world/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png (created)
  hello_world/android/app/src/main/res/values/styles.xml (created)
  hello_world/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png (created)
  hello_world/android/app/src/main/AndroidManifest.xml (created)
  hello_world/android/app/src/debug/AndroidManifest.xml (created)
  hello_world/android/gradle/wrapper/gradle-wrapper.properties (created)
  hello_world/android/gradle.properties (created)
  hello_world/android/.gitignore (created)
  hello_world/android/settings.gradle (created)
  hello_world/android/app/build.gradle (created)
  hello_world/android/app/src/main/kotlin/com/example/hello_world/MainActivity.k
  t (created)
  hello_world/android/build.gradle (created)
  hello_world/android/hello_world_android.iml (created)
  hello_world/pubspec.yaml (created)
  hello_world/README.md (created)
  hello_world/ios/Runner/Runner-Bridging-Header.h (created)
  hello_world/ios/Runner/AppDelegate.swift (created)
  hello_world/ios/Runner.xcodeproj/project.pbxproj (created)
  hello_world/lib/main.dart (created)
  hello_world/.idea/runConfigurations/main_dart.xml (created)
  hello_world/.idea/libraries/Flutter_for_Android.xml (created)
  hello_world/.idea/libraries/Dart_SDK.xml (created)
  hello_world/.idea/libraries/KotlinJavaRuntime.xml (created)
  hello_world/.idea/modules.xml (created)
  hello_world/.idea/workspace.xml (created)

然后执行到更新package依赖时,需要花费比较长时间:

Running "flutter pub get" in hello_world...  (This is taking an unexpectedly long time.) 

经过漫长的等待,发现报错了:

Got socket error trying to find package cupertino_icons at https://pub.dartlang.org.
pub get failed (server unavailable) -- attempting retry 1 in 1 second...                                           
Got socket error trying to find package cupertino_icons at https://pub.dartlang.org.                               
pub get failed (server unavailable) -- attempting retry 2 in 2 seconds...  

搜索了一下,原来是上网姿势不对,可以通过调整上网姿势或者使用镜像站解决。官方推荐使用镜像站:

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

依赖包更新完成后,会出现以下log:

All done!
[✓] Flutter: is fully installed. (Channel stable, v1.12.13+hotfix.8, on Mac OS X 10.15.3 19D76, locale zh-Hans-CN)
[!] Android toolchain - develop for Android devices: is partially installed; more components are available. (Android SDK
    version 28.0.3)
[✓] Xcode - develop for iOS and macOS: is fully installed. (Xcode 11.3.1)
[!] Android Studio: is partially installed; more components are available. (version 3.3)
[!] VS Code: is partially installed; more components are available. (version 1.32.1)
[!] Connected device: is not available.

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

In order to run your application, type:

  $ cd hello_world
  $ flutter run

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

log中可以看到,我的Android Studio和VS Code还有插件没有安装,先不急。先在iOS上跑起来:

cd hello_world
flutter run

果然又遇到问题,系统弹出框:无法打开“idevice_id”,因为无法验证开发者。


无法验证开发者

在安全与隐私中,可以看到拦截记录:


安全与隐私

点击“仍然允许”后重新执行:
flutter run

报错没有发现机器

No supported devices connected.

这是因为我没有启动模拟器,flutter无法和xcode一样,主动唤起模拟器,手动启动模拟器后重新执行 flutter run 即可:


Simulator Screen Shot - iPhone 8 - 2020-03-15 at 12.16.51.png

可以看到,flutter默认的demo已经可以跑起来了,但不是hello world这怎么行呢?用VS Code打开hello_world/lib/main.dart并按照提示安装dart插件,将marin.dart中的代码替换为以下代码:

// Copyright 2018 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: Center(
          child: Text('Hello World'),
        ),
      ),
    );
  }
}

运行结果:


hello world

看不懂代码?没关系,去学一下就可以了!

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

推荐阅读更多精彩内容