I'm trying to use a function(returning String) to a Text Widget. If I use getName() directly it works fine.
But if assign the function through a dynamic variable like below, It throws a error. I want to update the name on Button Press.
Text(title, style:kText20White),
Button(
title: 'Change Name',
onPressed: (){
setState((){
value = 1;
});
{
)
dynamic title;
int value = 0;
@override
void initState() {
super.initState();
title = getName;
}
String getName(){
if(name == 0){
return 'Taarak';
} else {
return 'Harshad';
}
Error:
type '() => String' is not a subtype of type 'String'
Is it possible to assign String by function through a variable?
Text(title(), ...)instead? TheTextwidget doesn't want a function, it wants a string.Future? Then useFutureBuilderTextwidget when text exists? You can makegetNamereturn nullable, and use conditional render: inside build,var name = getName()andif (name) Text(name)