Home > database >  I want string to be a widget in flutter
I want string to be a widget in flutter

Time:09-29

I mean that I want to get scores code from api:

enter image description here

CodePudding user response:

Well their is a widget called Text()

provide the String as it text then it will accept it

body: Text("any text")

CodePudding user response:

Use Text widget with string text

body: Text(text),

CodePudding user response:

As mentioned above, just simply use the Text widget as follows: body: Text(text)

CodePudding user response:

If this is for learning purposes and you want to know how to use a widget as a parameter you can do the following:

Text text = Text("Something"); // This way you specify exactly the type `Text`

or

Widget text = Text("Somethig"); // This way you can associate any widget to text

or

var text = Text("Something"); // var can be used with any type

Then you can use whenever you want in your code as long as it require a Widget as a child whitout any need for casting.

For you to create a widget that you can associate values to it you can go for something like this:

Widget myText({String value}) => Text(value);

There are many approaches depending on your need, and I strongly advice you to follow some tutorials for beginners first like Angela Yu bootcamp or any others and also use Flutter documentation.

CodePudding user response:

Try the following code:

body: Text(text),

CodePudding user response:

String is not and will not be a widget. What you are probably looking for is the widget to display a string in your UI: Text.

In your exemple, you can correct it like this :

return Scaffold(
   body: Text("Hello World")
);

See exemple on DartPad

  • Related