Dart: How to force string interpolation on a variable - flutter

I have a variable that contains a string with interpolated variables. In the code below, that variable is template. When I pass this variable to generateString function, I want to apply string interpolation on it because the values which interpolated variables require are available in generateString function only.
void main() {
String template = '<p>\${name}</p>';
var res = generateString(template);
}
generateString(template) {
var name = 'abc';
print(template);
return template;
}
The problem is when I am printing and returning template inside generateString fn, I am getting <p>${name}</p> instead of <p>abc</p>. Is there a way to explicitly tell the dart to so string interpolation?
I am new to Dart. I don't know if it is even possible to achieve or not. Please suggest how do I do this.
Edit: Based on the inputs from other users, I would like to make a clarification about the scenario presented. The value of template variable is not a string literal. I get that from UI as a user input. I have shown it here as a string literal for code simplicity. Also, please consider that name and template are not in the same scope in my scenario.

The other answers so far are wrong.
String interpolation (looking for $, etc) happens only while compiling from the source code to the value in memory. If that string in turn also has a $, it's no longer special.
It's not possible to trigger interpolation past the original compilation step. You can write a templating system that would look for something like {{name}} in the value, and replace it with the current value of name.

If you have the template and the variable in the same scope, it works as expected.
// evaluate variable inside ${}
var sport = 'basketball';
String template = 'I like <p>${sport}</p>';
print(template);

I didn't fully understand your question maybe this will help
void main() {
print(generateString('abc')); //<p>abc</p>
}
generateString(String template) {
return r"<p>" "$template" r"</p>";
}

Walter White here.
You must define the variable name as global var, so it can "cook" the string for you

Related

How can I get variable value from a text string?

I have a constants file with text values, for example:
const String loginIntro1 = "Login to account";
const String loginButton = "Login";
And I also have a Map containing similar info, for example:
{"loginIntro1":"Login to account","loginButton":"Login"}
The idea is that the Map (derived from a JSON file) takes precedence, but if the value doesn't exist, the constants value is used instead.
In my text widget, I want to grab the text by a method call, such as:
getText('loginIntro1');
Is this even possible? How can I get the value from the constants file with a String, rather than a direct reference to the variable? Perhaps I'm missing a much simpler way of achieving this!

How to take integer input from user in dart

i am new in dart and just want to know how to take integer input from user in dart with null safety. i found out a way to take number input from dart which is:
String? chossenNumber = stdin. readLineSync();
if(chossenNumber !=null)
{
int number = int.parse(chossenNumber);
}
but i am unable to use number variable outside of the scope. Please tell me a way to solve this issue.
You can define the variable at the top of the class and initialize it here so you will be able to use it everywhere in the class
The solution of it very simple just take input of number as String i.e
String? chossenNumber = stdin. readLineSync();
and when you want to use this variable parse it to the 'int' i.e
if(int.parse(chossenNumber) <100)
{
print("Your Statement");
}

Should I always use "this" in Dart?

Is it a good practice to use always 'this' in a method.
Even if there is no usage of the same variable name as input to this method.
String getStory(number) {
return this.storyData[number];
}
By doing this there won't have any conflicts with any input variable name (never).
Thank you in advance for your feedback.
Generally this. is only supposed to be used when there is ambiguity about what variable is being used.
class Test{
final int x;
Test(this.x);
}

Return value is not the same as predicted from a defined function

james=open('C:/Users/skora da_bura/Documents/data.txt')
jake=james.read()
james.close()
numblist=[]
charlist=[]
def Read(numblist,charlist):
for i in range(0,len(jake),4):
numblist.append(int(jake[i]))
for i in range(2,len(jake),4):
charlist.append(jake[i])
Bring = numblist,charlist
james = open('C:/Users/skora da_bura/Documents/data.txt')
jake22 = james.readlines()
james.close()
back='Number of lines read',len(jake22)
return back
print(Read([],[]))
print(charlist)
the charlist returns [] even though I had appended values to it to make a list when I was defining the function Read.
I don't seem to see what the problem is with the code
The charlist you define in the signature of Read shadows the global charlist. They're different variables that happen to have the same name. If you intend to modify the global variable, you shouldn't try to pass it as a parameter.

Can't Access Destructuring Assignment from Complex Object

Given the input value:
input =
name:'Foo'
id:'d4cbd9ed-fabc-11e6-83e6-307bd8cc75e3'
ref:5
addtData:'d4cbd9ed-fabc-11e6-83e6-307bd8cc75e3'
data:'bar'
When I try to destructure the input via a function like this:
simplify: (input)->
{ name, ref, id } = input
...the return value is still the full input or a copy of the input.
Am I missing something simple here? How can I access the destructured value. If you can't access the value via a return, it seems that destructuring has little value outside of locally scoped values.
While this isn't necessarily an advantage, the only way I was able to transpile and get the correct answer was to assign the destructure values to the local scope using # (aka this).
input =
name:'foo'
data:'bar'
id: 12314
key:'children'
ref:1
f = (input)->
{ #name, #id } = input
r = {}
f.call(r, input)
console.log r # Object {name: "foo", id: 12314}
working example - link
If someone has a better way to approach this, please add an answer so I can select it as this doesn't seem like the best way.