Home > Mobile >  Replace backSlash '\' with a normal slash '/' in dart
Replace backSlash '\' with a normal slash '/' in dart

Time:04-05

Hi guys I have an imageUrl with backSlashes('\\') I want to replaceAll the backSlashes with a normal slash ('/')

this is an imageUrl example : public\images\providerProfilePictures\2022-04-04T19-08-50.943Z-scaled_image_picker8914175492511161913.jpg

I want to make it with normal slashes like this one : public/images/providerProfilePictures/2022-04-04T19-08-50.943Z-scaled_image_picker8914175492511161913.jpg

this is my code :

 path = decoded['profilePictureUrl'];
  if (path!=''){
    path.replaceAll('/', '\\');
    path=ApiConstants.BASE_URL path;

    print(" my path now : " path);

I'm getting a wrong result with this function any help would be appreciated

CodePudding user response:

I think you got the 2 parameters swapped.

You have:

path.replaceAll('/', '\\');

It should be:

path.replaceAll('\\', '/');

Unrelated, but instead of if (path!='') you could write if (path.isNotEmpty) for improved readability.

Also, you can use the letter r in front of a String to make it verbatim.


Try this:

  final s = r'public\images\providerProfilePictures\2022-04-04T19-08-50.943Z-scaled_image_picker8914175492511161913.jpg';
  final url = s.replaceAll('\\', '/');
  print(url);

Console output:

flutter: public/images/providerProfilePictures/2022-04-04T19-08-50.943Z-scaled_image_picker8914175492511161913.jpg

  • Related