我之前一直在抱怨 flutter create 创建项目后一堆多余代码,原来是我没仔细看文档。 也有群友问我如何创建项目的时候指定 ios android 的开发语言,今天就一起来说下如何解决。

Flutter Create 命令行使用技巧

flutter-create-command-tips

视频

https://youtu.be/5W9-VjKJGDc

https://www.bilibili.com/video/BV1jBtoeHESA/

前言

原文 Flutter Create 命令行使用技巧

我之前一直在抱怨 flutter create 创建项目后一堆多余代码,原来是我没仔细看文档。

也有群友问我如何创建项目的时候指定 ios android 的开发语言,今天就一起来说下如何解决。

flutter-create-命令行

参考

正文

Flutter Create 命令行

输入 flutter create -h 可以查看所有的参数、选项。

$ flutter create -h

Create a new Flutter project.

If run on a project that already exists, this will repair the project, recreating any files that are missing.

Global options:
-h, --help                  Print this usage information.
-v, --verbose               Noisy logging, including all shell commands executed.
                            If used with "--help", shows hidden options. If used with "flutter doctor", shows additional diagnostic
                            information. (Use "-vv" to force verbose logging in those cases.)
-d, --device-id             Target device id or name (prefixes allowed).
    --version               Reports the version of this tool.
    --enable-analytics      Enable telemetry reporting each time a flutter or dart command runs.
    --disable-analytics     Disable telemetry reporting each time a flutter or dart command runs, until it is re-enabled.
    --suppress-analytics    Suppress analytics reporting for the current CLI invocation.

Usage: flutter create <output directory>
-h, --help                   Print this usage information.
    --[no-]pub               Whether to run "flutter pub get" after the project has been created.
                             (defaults to on)
    --[no-]offline           When "flutter pub get" is run by the create command, this indicates whether to run it in offline mode or
                             not. In offline mode, it will need to have all dependencies already available in the pub cache to succeed.
    --[no-]overwrite         When performing operations, overwrite existing files.
    --description            The description to use for your new Flutter project. This string ends up in the pubspec.yaml file.
                             (defaults to "A new Flutter project.")
    --org                    The organization responsible for your new Flutter project, in reverse domain name notation. This string is
                             used in Java package names and as prefix in the iOS bundle identifier.
                             (defaults to "com.example")
    --project-name           The project name for this new Flutter project. This must be a valid dart package name.
-i, --ios-language           The language to use for iOS-specific code, either Objective-C (legacy) or Swift (recommended).
                             [objc, swift (default)]
-a, --android-language       The language to use for Android-specific code, either Java (legacy) or Kotlin (recommended).
                             [java, kotlin (default)]
    --platforms              The platforms supported by this project. Platform folders (e.g. android/) will be generated in the target
                             project. This argument only works when "--template" is set to app or plugin. When adding platforms to a
                             plugin project, the pubspec.yaml will be updated with the requested platform. Adding desktop platforms
                             requires the corresponding desktop config setting to be enabled.
                             [ios (default), android (default), windows (default), linux (default), macos (default), web (default)]
-t, --template=<type>        Specify the type of project to create.

          [app]              (default) Generate a Flutter application.
          [module]           Generate a project to add a Flutter module to an existing Android or iOS application.
          [package]          Generate a shareable Flutter project containing modular Dart code.
          [plugin]           Generate a shareable Flutter project containing an API in Dart code with a platform-specific implementation
                             through method channels for Android, iOS, Linux, macOS, Windows, web, or any combination of these.
          [plugin_ffi]       Generate a shareable Flutter project containing an API in Dart code with a platform-specific implementation
                             through dart:ffi for Android, iOS, Linux, macOS, Windows, or any combination of these.
          [skeleton]         Generate a List View / Detail View Flutter application that follows community best practices.

-s, --sample=<id>            Specifies the Flutter code sample to use as the "main.dart" for an application. Implies "--template=app".
                             The value should be the sample ID of the desired sample from the API documentation website
                             (https://api.flutter.dev/). An example can be found at:
                             https://api.flutter.dev/flutter/widgets/SingleChildScrollView-class.html
-e, --[no-]empty             Specifies creating using an application template with a main.dart that is minimal, including no comments, as
                             a starting point for a new application. Implies "--template=app".
    --list-samples=<path>    Specifies a JSON output file for a listing of Flutter code samples that can be created with "--sample".

Run "flutter help" to see global options.

你可以养成这个喜欢,拿到一个外部命令工具,可以先 -h 来查看所有的功能。

创建标准项目

$ flutter create my_app

flutter crate

这是我们默认的创建方式,自带一个带按钮的界面。

创建一个空项目

$ flutter create --empty my_empty_app

flutter create --empty

好像这才是我需要的启动模版。

创建例子代码

flutter create --sample=<id> <project_name>

这个 id 在文档中说明,比如:

https://api.flutter.dev/flutter/material/AppBar-class.html

flutter create --sample=material.AppBar.1

添加平台支持

$ flutter create --platforms=web .

有的时候你会发现项目根目录下少个 web macos windows 这种目录。

你可以用项目根目录下执行,指定需要创建的平台代码。

创建不同类型的项目

$ flutter create -t app my_flutter_app

-t 就是指定类型, app 、module、package、plugin、skeleton。

指定平台开发语言

$ flutter create -i swift -a kotlin my_app

-i ios 的开发语言 objc swift

-a android 的开发语言 java kotlin

小结

在本文中,我们详细介绍了 Flutter Create 命令行技巧,旨在帮助开发者充分利用命令行工具创建和管理 Flutter 项目。通过学习这些实用的命令行技巧,您将能够显著提高开发效率,轻松应对项目中的各种挑战。无论您是初学者还是有经验的开发者,这些技巧都将为您的 Flutter 开发旅程提供实质性的帮助。

感谢阅读本文

如果有什么建议,请在评论中让我知道。我很乐意改进。


flutter 学习路径


© 猫哥 ducafecat.com

end