GetX™ 框架的官方 CLI 使用指南

GetX™ 框架的官方 CLI 使用指南

安装命令行工具

flutter pub global activate get_cli

配置环境变量

.zshrc

###
# Flutter GetX Cli
###
export PATH="$PATH":"$HOME/snap/flutter/.pub-cache/bin"

使环境变量生效

source .zshrc

验证安装是否成功

get --version

终端信息

The [--version] is not necessary

░██████╗░███████╗████████╗   ░█████╗░██╗░░░░░░██╗
██╔════╝░██╔════╝╚══██╔══╝   ██╔══██╗██║░░░░░░██║
██║░░██╗░█████╗░░░░░██║░░░   ██║░░╚═╝██║░░░░░░██║
██║░░╚██╗██╔══╝░░░░░██║░░░   ██║░░██╗██║░░░░░░██║
╚██████╔╝███████╗░░░██║░░░   ╚█████╔╝███████╗ ██║
░╚═════╝░╚══════╝░░░╚═╝░░░   ░╚════╝░╚══════╝ ╚═╝

Version: 1.7.1

Time: 200 Milliseconds

创建项目

get create project:flutter_get_01

终端信息

?  (Use arrow keys)
❯ Flutter Project
  Get Server
? What is your company's domain?  Example: com.yourcompany  com.dabolau
? what language do you want to use on ios? (Use arrow keys)
❯ Swift
  Objective-C
? what language do you want to use on android? (Use arrow keys)
❯ Kotlin
  Java
? Do you want to use null safe? (Use arrow keys)
❯ Yes!
  No
? do you want to use some linter? (Use arrow keys)
❯ no
  Pedantic [Deprecated]
  Effective Dart [Deprecated]
  Dart Recommended

Running `flutter create /Users/dabolau/temp/flutter_get_01` …

$ flutter create --no-pub -i swift -a kotlin --org com.dabolau /Users/dabolau/temp/flutter_get_01
Creating project ....
Wrote 128 files.

All done!
In order to run your application, type:

  $ cd .
  $ flutter run

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


Running `flutter pub get` …

$ flutter pub get
Running "flutter pub get" in flutter_get_01...                   1,127ms
$ dart migrate --apply-changes --skip-import-check
Migrating /Users/dabolau/temp/flutter_get_01

See https://dart.dev/go/null-safety-migration for a migration guide.

Analyzing project...
All sources appear to be already migrated.  Nothing to do.
✓  File: analysis_options.yaml created successfully at path: analysis_options.yaml
? Which architecture do you want to use? (Use arrow keys)
❯ GetX Pattern (by Kauê)
  CLEAN (by Arktekko)
? Your lib folder is not empty. Are you sure you want to overwrite your application? 
 WARNING: This action is irreversible (Use arrow keys)
❯ Yes!
  No
✓  'Package: get installed!
✓  File: main.dart created successfully at path: lib/main.dart
✓  File: home_controller.dart created successfully at path: lib/app/modules/home/controllers/home_controller.dart
✓  File: home_view.dart created successfully at path: lib/app/modules/home/views/home_view.dart
✓  File: home_binding.dart created successfully at path: lib/app/modules/home/bindings/home_binding.dart
✓  File: app_routes.dart created successfully at path: lib/app/routes/app_routes.dart
✓  File: app_pages.dart created successfully at path: lib/app/routes/app_pages.dart
✓  home route created successfully.
✓  Home page created successfully.
✓  GetX Pattern structure successfully generated.

Running `flutter pub get` …

$ flutter pub get
Running "flutter pub get" in flutter_get_01...                   1,027ms

Time: 42341 Milliseconds

目录结构

.
├── app
│   ├── data
│   ├── modules
│   │   └── home
│   │       ├── bindings
│   │       │   └── home_binding.dart
│   │       ├── controllers
│   │       │   └── home_controller.dart
│   │       └── views
│   │           └── home_view.dart
│   └── routes
│       ├── app_pages.dart
│       └── app_routes.dart
└── main.dart

创建页面

get create page:login

终端信息

✓  File: login_controller.dart created successfully at path: lib/app/modules/login/controllers/login_controller.dart
✓  File: login_view.dart created successfully at path: lib/app/modules/login/views/login_view.dart
✓  File: login_binding.dart created successfully at path: lib/app/modules/login/bindings/login_binding.dart
✓  login route created successfully.
✓  Login page created successfully.

Time: 742 Milliseconds

目录结构

.
├── app
│   ├── data
│   ├── modules
│   │   ├── home
│   │   │   ├── bindings
│   │   │   │   └── home_binding.dart
│   │   │   ├── controllers
│   │   │   │   └── home_controller.dart
│   │   │   └── views
│   │   │       └── home_view.dart
│   │   └── login
│   │       ├── bindings
│   │       │   └── login_binding.dart
│   │       ├── controllers
│   │       │   └── login_controller.dart
│   │       └── views
│   │           └── login_view.dart
│   └── routes
│       ├── app_pages.dart
│       └── app_routes.dart
└── main.dart

创建控制器 controller

get create controller:register on login

终端信息

✓  File: register_controller.dart created successfully at path: lib/app/modules/login/controllers/register_controller.dart
✓  The Register has been added to binding at path: lib/app/modules/login/bindings/login_binding.dart'

Time: 545 Milliseconds

目录结构

.
├── app
│   ├── data
│   ├── modules
│   │   ├── home
│   │   │   ├── bindings
│   │   │   │   └── home_binding.dart
│   │   │   ├── controllers
│   │   │   │   └── home_controller.dart
│   │   │   └── views
│   │   │       └── home_view.dart
│   │   └── login
│   │       ├── bindings
│   │       │   └── login_binding.dart
│   │       ├── controllers
│   │       │   ├── login_controller.dart
│   │       │   └── register_controller.dart
│   │       └── views
│   │           └── login_view.dart
│   └── routes
│       ├── app_pages.dart
│       └── app_routes.dart
└── main.dart

创建视图 view

get create view:register on login

终端信息

✓  File: register_view.dart created successfully at path: lib/app/modules/login/views/register_view.dart

Time: 500 Milliseconds

目录结构

.
├── app
│   ├── data
│   ├── modules
│   │   ├── home
│   │   │   ├── bindings
│   │   │   │   └── home_binding.dart
│   │   │   ├── controllers
│   │   │   │   └── home_controller.dart
│   │   │   └── views
│   │   │       └── home_view.dart
│   │   └── login
│   │       ├── bindings
│   │       │   └── login_binding.dart
│   │       ├── controllers
│   │       │   ├── login_controller.dart
│   │       │   └── register_controller.dart
│   │       └── views
│   │           ├── login_view.dart
│   │           └── register_view.dart
│   └── routes
│       ├── app_pages.dart
│       └── app_routes.dart
└── main.dart

创建状态控制器 provider

get create provider:register on login

终端信息

✓  File: register_provider.dart created successfully at path: lib/app/modules/login/providers/register_provider.dart

Time: 463 Milliseconds

目录结构

.
├── app
│   ├── data
│   ├── modules
│   │   ├── home
│   │   │   ├── bindings
│   │   │   │   └── home_binding.dart
│   │   │   ├── controllers
│   │   │   │   └── home_controller.dart
│   │   │   └── views
│   │   │       └── home_view.dart
│   │   └── login
│   │       ├── bindings
│   │       │   └── login_binding.dart
│   │       ├── controllers
│   │       │   ├── login_controller.dart
│   │       │   └── register_controller.dart
│   │       ├── providers
│   │       │   └── register_provider.dart
│   │       └── views
│   │           ├── login_view.dart
│   │           └── register_view.dart
│   └── routes
│       ├── app_pages.dart
│       └── app_routes.dart
└── main.dart

生成国际化文件 locales

在 assets/locales 目录创建 json 格式的语言文件

assets/locales/en_US.json

{
    "buttons": {
        "login": "Login",
        "sign_in": "Sign-in",
        "logout": "Logout"
    }
}

assets/locales/zh_CN.json

{
    "buttons": {
        "login": "登录",
        "sign_in": "注册",
        "logout": "注销"
    }
}

运行

get generate locales assets/locales               

终端信息

✓  File: locales.g.dart created successfully at path: lib/generated/locales.g.dart
✓  locale files generated successfully.

Time: 514 Milliseconds

输出

class AppTranslation {
  static Map<String, Map<String, String>> translations = {
    'zh_CN': Locales.zh_CN,
    'en_US': Locales.en_US,
  };
}

class LocaleKeys {
  LocaleKeys._();
  static const buttons_login = 'buttons_login';
  static const buttons_sign_in = 'buttons_sign_in';
  static const buttons_logout = 'buttons_logout';
}

class Locales {
  static const zh_CN = {
    'buttons_login': '登录',
    'buttons_sign_in': '注册',
    'buttons_logout': '注销',
  };
  static const en_US = {
    'buttons_login': 'Login',
    'buttons_sign_in': 'Sign-in',
    'buttons_logout': 'Logout',
  };
}

目录结构

.
├── app
│   ├── data
│   ├── modules
│   │   ├── home
│   │   │   ├── bindings
│   │   │   │   └── home_binding.dart
│   │   │   ├── controllers
│   │   │   │   └── home_controller.dart
│   │   │   └── views
│   │   │       └── home_view.dart
│   │   └── login
│   │       ├── bindings
│   │       │   └── login_binding.dart
│   │       ├── controllers
│   │       │   ├── login_controller.dart
│   │       │   └── register_controller.dart
│   │       ├── providers
│   │       │   └── register_provider.dart
│   │       └── views
│   │           ├── login_view.dart
│   │           └── register_view.dart
│   └── routes
│       ├── app_pages.dart
│       └── app_routes.dart
├── generated
│   └── locales.g.dart
└── main.dart

生成模型 model

在 assets/models 目录创建 json 格式的模型文件

assets/models/user.json

{
    "name": "",
    "age": 0,
    "friends": ["", ""]
}

运行

# 方式一,生成带有 provider 的 model
get generate model on login with assets/models/user.json
# 方式二,生成不带有 provider 的 model
get generate model on login with assets/models/user.json --skipProvider
# 方式三,通过 json 格式的 URL 生成 model
get generate model on login from "https://api.github.com/users/CpdnCristiano"

终端信息

✓  File: user_model.dart created successfully at path: lib/app/modules/login/user_model.dart
✓  File: user_provider.dart created successfully at path: lib/app/modules/login/providers/user_provider.dart

输出

class User {
  String? name;
  int? age;
  List<String>? friends;

  User({this.name, this.age, this.friends});

  User.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    age = json['age'];
    friends = json['friends'].cast<String>();
  }

  Map<String, dynamic> toJson() {
    final data = <String, dynamic>{};
    data['name'] = name;
    data['age'] = age;
    data['friends'] = friends;
    return data;
  }
}

目录结构

.
├── app
│   ├── data
│   ├── modules
│   │   ├── home
│   │   │   ├── bindings
│   │   │   │   └── home_binding.dart
│   │   │   ├── controllers
│   │   │   │   └── home_controller.dart
│   │   │   └── views
│   │   │       └── home_view.dart
│   │   └── login
│   │       ├── bindings
│   │       │   └── login_binding.dart
│   │       ├── controllers
│   │       │   ├── login_controller.dart
│   │       │   └── register_controller.dart
│   │       ├── providers
│   │       │   ├── register_provider.dart
│   │       │   └── user_provider.dart
│   │       ├── user_model.dart
│   │       └── views
│   │           ├── login_view.dart
│   │           └── register_view.dart
│   └── routes
│       ├── app_pages.dart
│       └── app_routes.dart
├── generated
│   └── locales.g.dart
└── main.dart

安装包 dependencies

运行

# 安装依赖包
get install http
# 安装多个依赖包
get install http path camera

终端信息

Installing package "http" …

✓  'Package: http installed!

Running `flutter pub get` …

$ flutter pub get
Running "flutter pub get" in flutter_get_01...                   1,566ms

Time: 4589 Milliseconds

安装包 dev_dependencies

运行

get install flutter_launcher_icons --dev

终端信息

The [--dev] is not necessary


Installing package "flutter_launcher_icons" …

✓  'Package: flutter_launcher_icons installed!

Running `flutter pub get` …

$ flutter pub get
Running "flutter pub get" in flutter_get_01...                      5.6s

Time: 8614 Milliseconds

移除包

# 移除依赖包
get remove http
# 移除多个依赖包
get remove http path camera

终端信息

Removing package: "http"

✓  Package: http removed!

Running `flutter pub get` …

$ flutter pub get
Running "flutter pub get" in flutter_get_01...                   1,422ms

Time: 3797 Milliseconds

更新脚手架

get update

终端信息

Latest version of get_cli already installed


Time: 814 Milliseconds

查看版本号

get -v

终端信息

░██████╗░███████╗████████╗   ░█████╗░██╗░░░░░░██╗
██╔════╝░██╔════╝╚══██╔══╝   ██╔══██╗██║░░░░░░██║
██║░░██╗░█████╗░░░░░██║░░░   ██║░░╚═╝██║░░░░░░██║
██║░░╚██╗██╔══╝░░░░░██║░░░   ██║░░██╗██║░░░░░░██║
╚██████╔╝███████╗░░░██║░░░   ╚█████╔╝███████╗ ██║
░╚═════╝░╚══════╝░░░╚═╝░░░   ░╚════╝░╚══════╝ ╚═╝

Version: 1.7.1

Time: 188 Milliseconds

查看帮助

get help

终端信息

List available commands:

  create:  
    controller:  Generate controller
    page:  Use to generate pages
    project:  Use to generate new project
    provider:  Create a new Provider
    screen:  Generate new screen
    view:  Generate view
  generate:  
    locales:  Generate translation file from json files
    model:  generate Class model from json
  help:  Show this help
  init:  generate the chosen structure on an existing project:
  install:  Use to install a package in your project (dependencies):
  remove:  Use to remove a package in your project (dependencies):
  sort:  Sort imports and format dart files
  update:  To update GET_CLI
  --version:  Shows the current CLI version'



Time: 119 Milliseconds

运行程序

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

推荐阅读更多精彩内容