Unable to get data from native android to flutter using platform channel?

511 views Asked by At

it prints the following to the console

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/

the first line is printed using println() in MainActivity.kt the second line is printed using print() in flutter

I even tried to get the length of the list using songsList.size in Kotlin and songsList.length in flutter . it shows 14 in Kotlin and it shows 1464 in flutter . One thing I know is that flutter is not getting the entire list even though I send it result.success(songsList). here is the code of Kotlin and flutter

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")
        }
    } 

flutter

  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;
  }
0

There are 0 answers