Flutter GetX系列教程---Cli使用以及常用命令
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
来快速创建一个页面,这个页面有controller
、view
、binding
、routes
等配置,结构是Getx_pattern
get create page:login
第二种:CLEAN
我们可以通过get create page:login
来快速创建一个页面,这个页面有controller
、view
、binding
、routes
等配置,结构是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