gpt4 book ai didi

flutter - 在flutter应用程序中显示git上次提交哈希和当前分支/标签

转载 作者:行者123 更新时间:2023-12-04 16:26:05 25 4
gpt4 key购买 nike

构建flutter应用程序时,如何访问源目录中的最后一个git提交哈希、当前分支和最后一个标签?我想在“关于版本”对话框中显示它。
https://pub.dev/packages/package_info但它没有关于 git 的信息。
是否有其他软件包可以提供此信息?

最佳答案

这可能不是最好的解决方案,但我是这样做的。
1. 将必要的文件添加到 Assets 中

flutter:
assets:
- .git/HEAD # This file points out the current branch of the project.
- .git/ORIG_HEAD # This file points to the commit id at origin (last commit id of the remote repository).
- .git/refs/heads/ # This directory includes files for each branch which points to the last commit id (local repo).
2. 从你的 .dart 代码中使用它
Future<String> getGitInfo() async {
final _head = await rootBundle.loadString('.git/HEAD');
final commitId = await rootBundle.loadString('.git/ORIG_HEAD');

final branch = _head.split('/').last;

print("Branch: $branch");
print("Commit ID: $commitId");

return "Branch: $branch, Commit ID: $commitId";
}
3. 如果需要,在您的 Flutter 应用程序中显示它
FutureBuilder<String>(
future: getGitInfo(),
builder: (context, snapshot) {
return Text(snapshot.data ?? "");
},
)
预期输出:
enter image description here
请注意, hot reload works also for assets ,因此它应该显示变化。

关于flutter - 在flutter应用程序中显示git上次提交哈希和当前分支/标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63888873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com