Home > Mobile >  how to replace string in dart
how to replace string in dart

Time:04-05

I have predefined url from server and want to replace the string in flutter dart.

For example as follow. I have no idea what is the route it is but I know that if I found ${track_id}, replace with real value. Same for ${artist_id} and ${album_id} and return the real url.

https://thitsarparami.org/artist/${artist_id}/album/${album_id}/track/${track_id} https://thitsarparami.org/${artist_id}/${album_id}/${track_id}

May I know how to replace dynamically.

CodePudding user response:

Check below example,

String refineUrl(String url) {

  if(url.contains("${album_id}")) {
    url = url.replaceAll("${album_id}", "<realAlbumId>");
  }
  // repeat the above check for all the dynamic values


  return url;
}

String url = "https://thitsarparami.org/artist/${artist_id}/album/${album_id}/track/${track_id}";

url = refineUrl(url);

Alternatively, you can send the albumId and other values through the function parameter.

  • Related