Flutter从0开发记账APP:二.创建第一个应用

前言

这里创建及其以后许多操作都是用 Terminal去操作,主要是命令方便。

Android StudioIntellij IDEA是一样的操作。

创建

名称要求

名称必须是小写,并满足[a-z0-9_]表达式

Running “flutter pub get” in gtboot…

这个运行慢问题,可以在环境变量中添加包地址,建议先添加

1
2
vim ~/.bash_profile
# 或者 vim ~/.bashrc

写入下面两行

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

然后刷新配置文件

1
2
3
source ~/.bash_profile   
# 查看当前是否成功 要关闭之前的创建程序
flutter -h

然后创建就很快了。

创建project

1
2
3
flutter create gtboot
# 这里创建的时候会生成一个 demo,比较浪费时间
cd gtboot

运行应用

如果使用的设备为手机,则需要开启usb调试模式,并且要对当前设备授权.

  • 检查Android设备是否在运行。如果没有显示, 请参照 设置.
1
flutter devices

也可以运行 flutter doctor 查看当前环境情况

1
2
3
4
5
6
7
8
9
10
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.17.4, on Linux, locale zh_CN.UTF-8)
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
! Some Android licenses not accepted. To resolve this, run: flutter doctor
--android-licenses
[!] Android Studio (not installed)
[!] Connected device
! No devices available

! Doctor found issues in 3 categories.

如果设备为手机,在IDEA上也可以看到。

  • 运行 flutter run 命令来运行应用程序:
1
flutter run

卡在Running Gradle task ‘assembleDebug’…

因为Gradle的Maven仓库是在国外, 我们可以使用阿里云的镜像地址~

修改 flutter 基础地址

1
vim /opt/flutter/packages/flutter_tools/gradle/flutter.gradle 

写入阿里云地址

1
2
3
4
5
6
7
8
9
10
11
12
buildscript {
repositories {
// google()
// jcenter()
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
}
}

修改项目中的地址

打开android/build.gradle文件

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
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
// google()
// jcenter()
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

allprojects {
repositories {
// google()
// jcenter()
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
}
}

重新运行项目

体验热加载

  1. 打开文件lib/main.dart
  2. 将字符串
    'You have pushed the button this many times:' 更改为
    'You have clicked the button this many times:'
  3. 不要按“停止”按钮; 让您的应用继续运行.
  4. 要查看您的更改,请调用 Save (cmd-s / ctrl-s), 或者点击 热重载按钮 (带有闪电图标的按钮).

当前体验热加载建议用编辑器启用,命令行启动时,命令行不是很能懂编辑器的保存操作。

推荐

CLUB

DEV

本文地址: https://github.com/maxzhao-it/blog/post/4508/