Flutter replace varable after string is build

291

As explained elsewhere, string interpolation in Dart is compile-time syntactic sugar. As briefly mentioned in The Dart Language Tour, string interpolation is performed on string literals. It is not performed on general String objects.

You will have to devise another approach to do what you want. For example:

String url = "";

data.forEach((key, value) {
  if (key == "\${id}") {
     key = id;
  } else if (key == "\${pass}") {
     key = pass;
  }
  url = "$url&$value=$key";
});

or a variation that might be better if you have more variables to deal with:

var allowedKeys = {
  "\${id}": id,
  "\${pass}": pass,
};

String url = "";

data.forEach((key, value) {
  key = allowedKeys[key] ?? key;
  url = "$url&$value=$key";
});

(Incidentally, your use of "key" and "value" seems confusing and backward from typical use.)

Share:
291
DIGITAL JEDI
Author by

DIGITAL JEDI

I am a Web and Android/IOS developer.

Updated on December 27, 2022

Comments

  • DIGITAL JEDI
    DIGITAL JEDI over 1 year

    I am trying to replace variable in a string which is dynamically generated within a loop

    String url = "";
    
    data.forEach((key, value) {
          url = "$url&$value=\${$key}";
    });
    

    Above method return something like this "https://example.com/test.php?user=${id}&pass=${pass}"

    I have two variable named id and pass. But after I generate this string dynamically it doesn't replaces the variable when I print it.

    var id = 1;
    var pass = abc;
    
    String url = "";
    
    data.forEach((key, value) {
          url = "$url&$value=\${$key}";
    });
    
    print(url);
    

    It should return "https://example.com/test.php?user=1&pass=abc"

    • OMi Shah
      OMi Shah about 3 years
      Instead of url = "$url&$value=\${$key}"; do url = "$url&$value=$key"; or url = "${url}&${value}=${key}";
    • A. Tratseuski
      A. Tratseuski about 3 years
      maybe because variable u r printing is 'URL' instead of 'url'?
    • DIGITAL JEDI
      DIGITAL JEDI about 3 years
      not working it just prints like this &pass=pass
    • DIGITAL JEDI
      DIGITAL JEDI about 3 years
      URL was the writing mistake I fixed that but still the same issue
    • OMi Shah
      OMi Shah about 3 years
      add your data object
    • Romeo
      Romeo about 3 years
      depending what widget are you performing this action in you should perform a setState({}) flutter.dev/docs/development/ui/interactive api.flutter.dev/flutter/widgets/State/setState.html this way your interface will be updated with the new values.
    • DIGITAL JEDI
      DIGITAL JEDI about 3 years
      my data variable is just an array like {id:user, pass:pass}
    • DIGITAL JEDI
      DIGITAL JEDI about 3 years
      "example.com/test.php?user=${id}&pass=${pass}" works if i print it directly but not if I build the URL with the loop dynamically. Main problem is this
    • OMi Shah
      OMi Shah about 3 years
      the way I told should be working. You might be doing something wrong. Post your complete code.
    • jamesdlin
      jamesdlin about 3 years
      Does this answer your question? Template file evaluation in flutter. Dart string interpolation is compile-time syntactic sugar; you cannot dynamically generate the template string.
    • DIGITAL JEDI
      DIGITAL JEDI about 3 years
      Its the complete code. I already told you that string is building correctly via loop but variables are not replacing. If I print the same string generate by loop in next line it works
    • DIGITAL JEDI
      DIGITAL JEDI about 3 years
      anyone? not ideas?