Home > other >  Flutter .replaceFirst (or all) is not working
Flutter .replaceFirst (or all) is not working

Time:09-02

  static String generateUri(Trip trip, String leading){
    String uri = "{leading}://trips?time={time}&origin={origin}&destination={destination}";
    uri.replaceAll("{leading}", leading)
    .replaceAll("{time}", trip.departurePlannedTime!.toIso8601String())
    .replaceAll("{origin}", trip.originLocation!.id.toString())
    .replaceAll("destination", trip.destinationLocation!.id.toString());
    return uri;
  }

As a side note, I need URIs with custom leading for deep linking, and I found nothing that creates this for me - uri.http doesn't really work for what I need here.

That being said: I am trying to create a deep link for the app I am working on, that however is not seeming to work, running the code above gives me exactly and unchangedly the same String I defined in line 2.

More guessing than with anything in mind I tried changing replaceAll with replaceFirst (should not make a difference in this case since there is only one occurance)?

I worked with .replace before so I am not sure why this isn't working. I also tried separating the things in {} from the rest of the String with blankspaces, but that also didn't do anything (would've surprised me if it did but it was just a guess).

CodePudding user response:

replaceAll() return result as string and does not modify the existing one. This should work

  static String generateUri(Trip trip, String leading){
    String uri = "{leading}://trips?time={time}&origin={origin}&destination={destination}";

    return uri.replaceAll("{leading}", leading)
    .replaceAll("{time}", trip.departurePlannedTime!.toIso8601String())
    .replaceAll("{origin}", trip.originLocation!.id.toString())
    .replaceAll("destination", trip.destinationLocation!.id.toString());
  }
  • Related