今天尝试写第一个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/[email protected]
(created)
hello_world/ios/Runner/Assets.xcassets/LaunchImage.imageset/[email protected]
(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/[email protected]
g (created)
hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/[email protected]
g (created)
hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/[email protected]
g (created)
hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/[email protected]
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/[email protected]
g (created)
hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json
(created)
hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/[email protected]
g (created)
hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/[email protected]
g (created)
hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/[email protected]
g (created)
hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/[email protected]
g (created)
hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/[email protected]
g (created)
hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/[email protected]
g (created)
hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/[email protected]
g (created)
hello_world/ios/Runner/Assets.xcassets/AppIcon.appiconset/[email protected]
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 即可:
可以看到,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'),
),
),
);
}
}
运行结果:
看不懂代码?没关系,去学一下就可以了!