【问题标题】:How to "refresh" dependency packages with main project in Flutter?如何在 Flutter 中使用主项目“刷新”依赖包?
【发布时间】:2022-01-13 06:52:28
【问题描述】:

我是 Flutter 开发的新手。我试图将我的代码作为依赖项分成多个本地包。这是我目前的项目结构:

/packages/commons: 包含常用小部件和实用功能的包

/packages/fruits:一个包含水果屏幕的包(取决于:commons

/main: 取决于commons & fruits

每当我在commons 包中进行影响fruits 包的依赖项更改¹ 时,我必须在三个文件夹(对于commonsfruits 和主项目)中执行flutter pub get 才能能够运行代码。

有什么办法可以将这个过程减少到一次“刷新”点击?


  1. commons 中的示例:
flutter pub add fluro
flutter pub get

【问题讨论】:

  • 您可能对mono_repo 或替代品感兴趣。

标签: flutter pubspec


【解决方案1】:

因为我不耐烦了,所以我想为此编写一个小的 shell 脚本。我不知道是否有更简单的方法,但这对我有用。

我的项目有以下文件夹结构:

project
   pubspec.yaml
   packages/
       package1/
           pubspec.yaml
       package2/
           pubspec.yaml
       package3/
           pubspec.yaml

我在项目文件夹中创建了一个refresh.bash 文件。下面是它的样子:

# open project directory or exit if failed
cd PROJECT_PATH || exit
# check if project contains pubspec.yaml file
if [ -f "pubspec.yaml" ]; then
    # check if packages folder exists
    if [ -d "packages" ]; then
        # open packages folder
        cd "packages" || exit
        # run for all subdirectories of packages folder
        for d in */; do
            # open subdirectory
            cd "$d" || exit
            # check if subdirectory contains pubspec.yaml file
            if [ -f "pubspec.yaml" ]; then
                # run pub get for subdirectory (package)
                flutter pub get
            fi
            # exit subdirectory
            cd ..
        done
        # exit packages directory
        cd ..
    fi

    # run pub get for project directory
    flutter pub get
else
    echo "pubspec.yaml not found"
fi

PROJECT_PATH 替换为您的项目路径。

为了能够运行这个脚本,我必须让它可执行。这可以通过以下命令完成:

chmod +x refresh.bash

在此之后,我运行脚本(在 bash 终端中):

./refresh.bash

注意:我不知道 shell 脚本是如何工作的,我从 Shell Script Cheatsheet 那里得到了一些提示。所以,如果有任何问题,或者如果它可以更小,请建议我。这是我使用 shell 脚本的第一天。

【讨论】:

    猜你喜欢
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    相关资源
    最近更新 更多