Home > Software engineering >  Flutter get string from list based on index number
Flutter get string from list based on index number

Time:06-29

I have list of string data like:

[
    https://example.com/app/audio/14/5/1.mp3,
    https://example.com/app/audio/14/5/2.mp3,
    https://example.com/app/audio/14/5/3.mp3,
    https://example.com/app/audio/14/5/4.mp3,
    https://example.com/app/audio/14/5/5.mp3,
    https://example.com/app/audio/14/5/6.mp3,
    https://example.com/app/audio/14/5/7.mp3,
    https://example.com/app/audio/14/5/8.mp3,
    https://example.com/app/audio/14/5/9.mp3,
    https://example.com/app/audio/14/5/10.mp3,
    https://example.com/app/audio/14/5/11.mp3,
    https://example.com/app/audio/14/5/12.mp3,
    https://example.com/app/audio/14/5/13.mp3,
    https://example.com/app/audio/14/5/14.mp3,
    https://example.com/app/audio/14/5/15.mp3
]

And I'm looking for solution to get specific string from this list. For instance if want to get number 5, it should return https://example.com/app/audio/14/5/5.mp3 string.

I know you might say it starts from 0 so string number 5 is https://example.com/app/audio/14/5/6.mp3 and not https://example.com/app/audio/14/5/5.mp3 but I'm looking for that specific name rather than row index.

Code

Here is what I have so far:

// Load file
String file = currentLang == "id" ? "assets/lists/a-player-id.json" :
currentLang == "fa" ? "assets/lists/a-player-fa.json" :
"assets/lists/a-player-en.json";
String data = await DefaultAssetBundle.of(context).loadString(file);
var jsonResult = jsonDecode(data);


// Get data from loaded file
var playlist;
var audios;
for(var i = 0; i < jsonResult.length; i  ) {
    if(jsonResult[i]['name'] == bookName || jsonResult[i]['name_en'] == bookName || jsonResult[i]['name_fa'] == bookName){
        playlist = jsonResult[i]['playList'];
        audios = jsonResult[i]['audios']; // sample data is presented at the top of question.
    }
}

// Get specific string from "audios list"
var myStaticNumber = 5;
// Here I need help to get myStaticNumber from audios list.

Any suggestion?

CodePudding user response:

You are propably looking for this :

StringList[myStaticNumber-1];
  • Related