1

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?

6
  • Text(title(), ...) instead? The Text widget doesn't want a function, it wants a string. Commented Mar 18, 2021 at 9:37
  • You can provide a function until it returns String. Commented Mar 18, 2021 at 9:38
  • I don't understand what you're saying ("until it returns?), can you reword it? Oh, you want to await a network request, asynchronous event or Future? Then use FutureBuilder Commented Mar 18, 2021 at 9:39
  • Sorry for my English. I meant 'as long as it returns' String from the function. I don't need to use FutureBuilder since my Strings are static. Check getName() for reference. Commented Mar 18, 2021 at 9:49
  • You only want to show Text widget when text exists? You can make getName return nullable, and use conditional render: inside build, var name = getName() and if (name) Text(name) Commented Mar 18, 2021 at 9:51

2 Answers 2

2

Finally I understood what you want to do.

You need to check title's variable type and divide calling by type.

Here is final solution!!!

Text(title is String ? title : 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';
 }
Sign up to request clarification or add additional context in comments.

4 Comments

This will not help my use case. I've edited my question for better understanding of my usecase.
I want to assign title with String variable later, but providing () to title will not let me do that. Is it not possible to pass function directly via variable without () to mention on Text widget?
I updated content after understood what you want to do.
Enjoy your coding~!
0

You are referencing a function, You have to call function there

String title;

String getName(){
 return 'First Name';
}

title = getName; //Here you are referencing a function, You have to call function here

Text(title, style:kText20White)

Solution:

title = getName();

Try this.

2 Comments

This will assign the output of getName() to the title, which is non-updatable later. I've edited my question for better understanding of my usecase.
@VipiNNegi in this case you just have to give variable type annotation as a String, Use String instead of dynamic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.