Flutter GetX系列教程---Cli使用以及常用命令

玉米炖排骨發表於2024-03-13

GetX Cli安裝

第一步:安裝Cli腳手架

我們透過命令flutter pub global activate get_cli 進行腳手架的全域性安裝,本文以MacOS為例。

 flutter pub global activate get_cli
+ _fe_analyzer_shared 61.0.0 (67.0.0 available)
+ analyzer 5.13.0 (6.4.1 available)
+ ansicolor 2.0.2
+ archive 3.4.10
+ args 2.4.2
+ async 2.11.0
+ basic_utils 5.5.4 (5.7.0 available)
+ boolean_selector 2.1.1
+ characters 1.3.0
+ charcode 1.3.1
+ chunked_stream 1.4.2
+ circular_buffer 0.11.0
+ cli_util 0.4.1
+ clock 1.1.1
+ collection 1.18.0
+ convert 3.1.1
+ coverage 1.6.4 (1.7.2 available)
+ crypto 3.0.3
+ csv 5.1.1 (6.0.0 available)
+ dart_console2 3.0.0
+ dart_style 2.3.2 (2.3.6 available)
+ dcli 2.3.0 (3.3.6 available)
+ dcli_core 2.3.0 (3.3.6 available)
+ equatable 2.0.5
+ ffi 2.1.2
+ file 6.1.4 (7.0.0 available)
+ frontend_server_client 3.2.0
+ functional_data 1.1.1
+ get_cli 1.8.4
+ glob 2.1.2
+ globbing 1.0.0
+ http 0.13.6 (1.2.1 available)
+ http_multi_server 3.2.1
+ http_parser 4.0.2
+ ini 2.1.0
+ intl 0.18.1 (0.19.0 available)
+ io 1.0.4
+ js 0.6.7 (0.7.1 available)
+ json2yaml 3.0.1
+ json_annotation 4.8.1
+ logging 1.2.0
+ matcher 0.12.16+1
+ meta 1.12.0
+ mime 1.0.5
+ node_preamble 2.0.2
+ package_config 2.1.0
+ path 1.9.0
+ pointycastle 3.7.4
+ pool 1.5.1
+ posix 5.0.0 (6.0.1 available)
+ process_run 0.12.5+3 (0.14.2 available)
+ pub_semver 2.1.4
+ pubspec 2.3.0
+ pubspec2 2.4.2 (4.0.0 available)
+ pubspec_lock 3.0.2
+ quiver 3.2.1
+ random_string 2.3.1
+ recase 4.1.0
+ scope 3.0.0 (4.1.0 available)
+ settings_yaml 6.0.0 (8.0.1 available)
+ shelf 1.4.1
+ shelf_packages_handler 3.0.2
+ shelf_static 1.1.2
+ shelf_web_socket 1.0.4
+ source_map_stack_trace 2.1.1
+ source_maps 0.10.12
+ source_span 1.10.0
+ stack_trace 1.11.1
+ stream_channel 2.1.2
+ string_scanner 1.2.0
+ sum_types 0.3.5
+ synchronized 3.1.0+1
+ system_info2 4.0.0
+ term_glyph 1.2.1
+ test_api 0.6.1 (0.7.0 available)
+ test_core 0.5.9 (0.6.0 available)
+ typed_data 1.3.2
+ uri 1.0.0
+ uuid 3.0.7 (4.3.3 available)
+ validators2 5.0.0
+ version 3.0.2
+ vm_service 11.10.0 (14.1.0 available)
+ watcher 1.1.0
+ web 0.5.1
+ web_socket_channel 2.4.4
+ webkit_inspection_protocol 1.2.1
+ win32 4.1.4 (5.3.0 available)
+ yaml 3.1.2
Building package executables... (23.8s)
Built get_cli:get.
Installed executables get and getx.
Warning: Pub installs executables into $HOME/.pub-cache/bin, which is not on your path.
You can fix that by adding this to your shell's config file (.bashrc, .bash_profile, etc.):

  export PATH="$PATH":"$HOME/.pub-cache/bin"

Activated get_cli 1.8.4.

第二步:設定環境變數

一般Mac的環境變數都是透過根目錄的 code ~/.zshrc 進行環境變數設定。

#getX
export PATH="$PATH":"$HOME/.pub-cache/bin"

第三步:使設定的環境變數生效

我們可以透過source 加上路徑,對環境變數生效。

source ~/.zshrc

第四步:校驗是否成功安裝

我們透過get命令,看能不能列印如下,如果有,那麼恭喜你安裝成功了。

Cli建立工程

我們可以透過get create project來進行建立工程,

get create projectr

然後出現報錯提示

 1) Flutter Project
  2) Get Server
Select which type of project you want to create ? [1] Unhandled exception:
Synchronous waiting using dart:cli waitFor and C API Dart_WaitForEvent is deprecated and disabled by default. This feature will be fully removed in Dart 3.4 release. You can currently still enable it by passing --enable_deprecated_wait_for to the Dart VM. See https://dartbug.com/52121.
dart:cli-patch/cli_patch.dart 9:47                                             _waitForEvent
dart:cli/wait_for.dart 64:12                                                   _WaitForUtils.waitForEvent
dart:cli/wait_for.dart 152:19                                                  waitFor
package:dcli/src/util/wait_for_ex.dart 38:17                                   waitForEx
package:dcli/src/functions/echo.dart 34:5                                      _Echo.echo
package:dcli/src/functions/echo.dart 24:13                                     echo
package:dcli/src/functions/ask.dart 157:7                                      Ask._ask
package:dcli/src/functions/ask.dart 113:11                                     ask
package:dcli/src/functions/menu.dart 135:22                                    menu
package:get_cli/common/menu/menu.dart 20:20                                    Menu.choose
package:get_cli/commands/impl/create/project/project.dart 27:25                CreateProjectCommand.execute
../../../.pub-cache/hosted/pub.flutter-io.cn/get_cli-1.8.4/bin/get.dart 18:23  main
dart:isolate-patch/isolate_patch.dart 295:33                                   _delayEntrypointInvocation.<fn>
dart:isolate-patch/isolate_patch.dart 184:12                                   _RawReceivePort._handleMessage
package:dcli/src/util/wait_for_ex.dart 35:28                                   waitForEx
package:dcli/src/functions/echo.dart 34:5                                      _Echo.echo
package:dcli/src/functions/echo.dart 24:13                                     echo
package:dcli/src/functions/ask.dart 157:7                                      Ask._ask
package:dcli/src/functions/ask.dart 113:11                                     ask
package:dcli/src/functions/menu.dart 135:22                                    menu
package:get_cli/common/menu/menu.dart 20:20                                    Menu.choose
package:get_cli/commands/impl/create/project/project.dart 27:25                CreateProjectCommand.execute
../../../.pub-cache/hosted/pub.flutter-io.cn/get_cli-1.8.4/bin/get.dart 18:23  main
dart:isolate-patch/isolate_patch.dart 295:33                                   _delayEntrypointInvocation.<fn>
dart:isolate-patch/isolate_patch.dart 184:12

解決方法:

flutter pub global activate  --source=git https://github.com/inyong1/get_cli.git

然後出現如下提示,我們選擇建立Flutter Project

 get create project                                             ✔  took 9s   at 11:44:07 PM 

  1) Flutter Project
  2) Get Server
Select which type of project you want to create ? [1] 1
what is the name of the project? getx_demo
What is your company's domain?  Example: com.yourcompany  com.mk

  1) Swift
  2) Objective-C
what language do you want to use on ios? [1]

  1) Kotlin
  2) Java
what language do you want to use on android? [1]

  1) yes
  2) no
do you want to use some linter? [1]

Running `flutter create /Users/mac/Desktop/Practice/FlutterProjects/getx/getx_demo` …

$ flutter create --no-pub -i swift -a kotlin --org com.mk /Users/mac/Desktop/Practice/FlutterProjects/getx/getx_demo
Developer identity "Apple Development: ypf201410@163.com (N36QYVS954)" selected for iOS code signing
Creating project ....
Wrote 129 files.

All done!
You can find general documentation for Flutter at: https://docs.flutter.dev/
Detailed API documentation is available at: https://api.flutter.dev/
If you prefer video documentation, consider: https://www.youtube.com/c/flutterdev

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
Resolving dependencies...
+ async 2.11.0
+ boolean_selector 2.1.1
+ characters 1.3.0
+ clock 1.1.1
+ collection 1.18.0
+ cupertino_icons 1.0.6
+ fake_async 1.3.1
+ flutter 0.0.0 from sdk flutter
+ flutter_lints 3.0.1
+ flutter_test 0.0.0 from sdk flutter
+ leak_tracker 10.0.0 (10.0.4 available)
+ leak_tracker_flutter_testing 2.0.1 (3.0.3 available)
+ leak_tracker_testing 2.0.1 (3.0.1 available)
+ lints 3.0.0
+ matcher 0.12.16+1
+ material_color_utilities 0.8.0 (0.11.0 available)
+ meta 1.11.0 (1.12.0 available)
+ path 1.9.0
+ sky_engine 0.0.99 from sdk flutter
+ source_span 1.10.0
+ stack_trace 1.11.1
+ stream_channel 2.1.2
+ string_scanner 1.2.0
+ term_glyph 1.2.1
+ test_api 0.6.1 (0.7.0 available)
+ vector_math 2.1.4
+ vm_service 13.0.0 (14.1.0 available)
Changed 27 dependencies!
7 packages have newer versions incompatible with dependency constraints.
Try `flutter pub outdated` for more information.
✓  'Package: flutter_lints installed!
✓  File: analysis_options.yaml created successfully at path: analysis_options.yaml

  1) GetX Pattern (by Kauê)
  2) CLEAN (by Arktekko)
Which architecture do you want to use? [1]

  1) Yes!
  2) No
Your lib folder is not empty. Are you sure you want to overwrite your application?
 WARNING: This action is irreversible [1]
✓  '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
Resolving dependencies...
+ get 4.6.6
  leak_tracker 10.0.0 (10.0.4 available)
  leak_tracker_flutter_testing 2.0.1 (3.0.3 available)
  leak_tracker_testing 2.0.1 (3.0.1 available)
  material_color_utilities 0.8.0 (0.11.0 available)
  meta 1.11.0 (1.12.0 available)
  test_api 0.6.1 (0.7.0 available)
  vm_service 13.0.0 (14.1.0 available)
Changed 1 dependency!
7 packages have newer versions incompatible with dependency constraints.
Try `flutter pub outdated` for more information.

Time: 427628 Milliseconds

如果能看到如上就證明透過Cli建立工程成功了。

Cli建立頁面

第一種:Getx_pattern

cd getx_demo工程目錄下, 我們可以透過get create page:login來快速建立一個頁面,這個頁面有controllerviewbindingroutes等配置,結構是Getx_pattern

get create page:login

第二種:CLEAN

我們可以透過get create page:login來快速建立一個頁面,這個頁面有controllerviewbindingroutes等配置,結構是CLEAN

Cli建立控制器

我們可以透過get create controller:login來快速為已存在的page進行建立controller,並且他還重新了controller的生命週期以及初始化方法。

get create controller:another on home 

Cli建立View

如果我們只是想單獨建立一個View,那我們可以透過get create view:alogin on login來進行建立。

get create view:alogin on login 

  

Cli建立Provider

如果我們需要建立GetConnect,那我們可以透過get create provider:blogin on login來進行建立。

get create provider:blogin on login  

Cli建立國際化

如果我們需要對應用進行國際化配置,那我們可以透過get generate locales assets/locales來進行建立。

get generate locales assets/locales

Cli透過json生成模型

第一種:不僅建立模型類,還會提供Provider

如果我們需要對某個json檔案生成模型,那我們可以透過get generate model on home with assets/models/user.json來進行生成

get generate model on home with assets/models/user.json  

第二種:只生成模型類

get generate model on login with assets/models/user.json --skipProvider

  

第三種:透過連線生成模型類

get generate model on home from "https://api.github.com/users/CpdnCristiano"

  

Cli安裝包(dependencies)

第一種:直接安裝最新版本

get install dio

  

第二種:同時安裝多個包

get install path dio

  

第三種:安裝自定版本的包

get install prodiver:5.0.0

  

Cli安裝包(dev_dependencies)

我們可以透過get install flutter_launcher_icons --dev安裝開發時所依賴的包

get install flutter_launcher_icons --dev

  

Cli解除安裝包

第一種:解除安裝某個安裝包

get remove http

  

第二種:同時解除安裝多個包

get remove dio path

  

Cli更新腳手架

我們可以透過get update對腳手架進行更新

get update

  

Cli檢視版本號

我們可以透過get -v 檢視當前腳手架的版本號

get -v

  

Cli幫助

當我們忘記了命令的使用方式,我們可以透過get help進行檢視幫助。

get help

  

總結

以上就是對Cli的腳手架命令的一個實踐和總結,相對於自己手動建立結構,用Cli就可以很輕鬆的實現。

摘抄自:https://liujunmin.com/flutter/getx/getx_cli.html

相關文章