こんにちはEveryDaySoft代表の永田です。
Flutter内で画像などを格納するディレクトリーasstesにおいて、
URL情報の取得方法を紹介いたします。
Flutterの技術だけですとassetsの情報取得する機能はないので、pythonスクリプトを使用しました。
挙動
起動時にPythonスクリプトでFlutterのasstes bundleの情報を取得して、dartファイルを作成して書き込めます。
VSCでbuild時に書き込む必要があるので、ファイルは作成済みにしていますが、コマンド実行するとファイルは自動生成します。
https://drive.google.com/file/d/1N5vkKb76HxJGNgB0KaNlyR7PQgYRbATe/view?usp=sharing
挙動2
取得したURLから音声を作成する様子
アプリの挙動
- プロジェクトのルートディレクトリに
build.sh
という名前のシェルスクリプトファイルを作成してください。 build.sh
ファイルに以下の内容を追加します。
ディレクトリ、dartファイルを指定して、asstes内にあるfileをasset_list.dartに書き込みます。
#!/bin/bash
ASSET_DIR="assets/"
OUTPUT_FILE="lib/asset_list.dart"
echo "final List assetList = [" > $OUTPUT_FILE
for file in $(find $ASSET_DIR -type f); do
asset_path=$(echo $file | sed "s|\\\\|/|g")
echo " '$asset_path'," >> $OUTPUT_FILE
done
echo "];" >> $OUTPUT_FILE
VSCodeでrun時の挙動
jsonファイルの作成

launch.jsonのスクリプト
Flutter: Build and Runを追加してます。
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "flutter_sound_audio",
"request": "launch",
"type": "dart"
},
{
"name": "flutter_sound_audio (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "flutter_sound_audio (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
},
{
"name": "Flutter: Build and Run",
"request": "launch",
"type": "dart",
"preLaunchTask": "build_and_run"
},
]
}
tasks.jsonのスクリプト
{
"version": "2.0.0",
"tasks": [
{
"label": "build_and_run",
"type": "shell",
"command": "./build.sh && flutter run",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Flutter runが行われていますが、ホットリロードする際は実施しているターミナルでRを押すと、ホットリロード機能を実行できます。
貴重なお時間お読みくださいまして、誠にありがとうございます。
コメントを残す