以下をコンソールに出力します
I/System.out: [{file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20181028-WA0004.mp3, file_name=AUD-20181028-WA0004.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20200507-WA0009.mp3, file_name=AUD-20200507-WA0009.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20200507-WA0012.mp3, file_name=AUD-20200507-WA0012.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20181028-WA0005.mp3, file_name=AUD-20181028-WA0005.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20180624-WA0001.mp3, file_name=AUD-20180624-WA0001.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20200310-WA0005.mp3, file_name=AUD-20200310-WA0005.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20180904-WA0004.mp3, file_name=AUD-20180904-WA0004.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20200507-WA0011.mp3, file_name=AUD-20200507-WA0011.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20200507-WA0008.mp3, file_name=AUD-20200507-WA0008.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20180807-WA0017.mp3, file_name=AUD-20180807-WA0017.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20181029-WA0000.mp3, file_name=AUD-20181029-WA0000.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20200507-WA0010.mp3, file_name=AUD-20200507-WA0010.mp3}]
I/flutter: [{file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20181028-WA0004.mp3, file_name=AUD-20181028-WA0004.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20200507-WA0009.mp3, file_name=AUD-20200507-WA0009.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20200507-WA0012.mp3, file_name=AUD-20200507-WA0012.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20181028-WA0005.mp3, file_name=AUD-20181028-WA0005.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20180624-WA0001.mp3, file_name=AUD-20180624-WA0001.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20200310-WA0005.mp3, file_name=AUD-20200310-WA0005.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20180904-WA0004.mp3, file_name=AUD-20180904-WA0004.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/WhatsApp Audio/AUD-20200507-WA0011.mp3, file_name=AUD-20200507-WA0011.mp3}, {file_path=/storage/emulated/0/WhatsApp/Media/
最初の行は、MainActivity.kt で println() を使用して印刷されます。2 行目は、フラッターで print() を使用して印刷されます。
Kotlin の songList.size と flutter の songList.length を使用して、リストの長さを取得しようとさえしました。Kotlin では 14 を示し、 flutter では 1464 を示しています。私が知っていることの1つは、result.success(songsList)を送信しても、フラッターがリスト全体を取得していないことです。ここにKotlinとフラッターのコードがあります
val songsList = getSongsList(Environment.getExternalStorageDirectory().absolutePath)
println(songsList)
result.success(songsList.toString())
private fun getSongsList(rootPath:String) : ArrayList<HashMap<String, String>> {
val fileList: ArrayList<HashMap<String, String>> = ArrayList()
try {
val rootFolder = File(rootPath)
val files: Array<File> = rootFolder.listFiles()
for (file in files) {
if (file.isDirectory) {
fileList.addAll(getSongsList(file.absolutePath))
} else if (file.name.endsWith(".mp3")) {
val song: HashMap<String, String> = HashMap()
song["file_path"] = file.absolutePath
song["file_name"] = file.name
fileList.add(song)
}
}
return fileList
} catch (e: Exception) {
throw Error("failed to load data $e")
}
}
フラッター
Future<List<Song>> _getSongsList() async {
print("calling get songs list");
final String songlist = await platform.invokeMethod("getSongsList");
print(songlist);
print(songlist.length);
var parsed = json.decode(songlist);
List<Song> sList = parsed.map<Song>((json)=> new Song.fromJson(json)).toList();
print(sList.toString());
return sList;
}