I simplified my code so my question would be easier to ask. Basically, I have a value that goes through an if else statement and then text is printed based on that value.
class Number {
static void compareVal () {
final int a = 5;
if (a == 1) {
print('Number is 1');
//String i = 'Number is 1';
}
else {
print('Number is not 1');
//String i = 'Number is 1';
}
}
}
class RequirementTenWidgetState extends State<RequirementTenWidget>{
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: double.infinity,
height: double.infinity,
child: const Text('Number',
style: TextStyle(
fontSize: 50,
color: Colors.white,
),
textAlign: TextAlign.center,
),
),
floatingActionButton: FloatingActionButton(
child: Text('something'),
backgroundColor: Colors.blue[600],
onPressed: () => {
Number.caompareVal()
},
),
);
`
I wanted to add a button that when pressed displays the text on the screen, currently when the button is pressed, the text displays on the terminal and not in the app. How would I get the app to display the text?
2
Answers
For that you have to (1) take one string value which display your text (2) make your number function return type String. (3) Update the string based on that.
Here is your updated code :
in-order to show text on the app, use
Text()
widgetI am changed the
compareVal()
return type toString
, and kept in the same class for testing, you can use anywhere, just call accordinglyto update the text in realtime used
setState()
method