Flutter replace varable after string is build
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.)
Comments
-
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 about 3 yearsInstead of
url = "$url&$value=\${$key}";
dourl = "$url&$value=$key";
orurl = "${url}&${value}=${key}";
-
A. Tratseuski about 3 yearsmaybe because variable u r printing is 'URL' instead of 'url'?
-
DIGITAL JEDI about 3 yearsnot working it just prints like this &pass=pass
-
DIGITAL JEDI about 3 yearsURL was the writing mistake I fixed that but still the same issue
-
OMi Shah about 3 yearsadd your
data
object -
Romeo about 3 yearsdepending 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 about 3 yearsmy data variable is just an array like {id:user, pass:pass}
-
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 about 3 yearsthe way I told should be working. You might be doing something wrong. Post your complete code.
-
jamesdlin about 3 yearsDoes 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 about 3 yearsIts 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 about 3 yearsanyone? not ideas?
-