Reputation: 3958
I am trying to write an if/else statement in dart on my flutter app. I am trying to see if the passed id is equal to 3 to return a whole page of code, and else would be a different page. for example...
Widget build(BuildContext context) {
final TextEditingController controller = new TextEditingController();
String result = "";
If (${widget.id} = 3){
return Scaffold(
all of scaffold 1)
}; else {
return Scaffold(
all of scaffold 2)
};
Do i need to set ${widget.id}
to a variable to call into the if statement? And where would i set it in the .dart page, in void initstate(){}
?
Upvotes: 3
Views: 10767
Reputation: 10759
You can also do it like this
Using a ternary operator
Widget build(BuildContext context) {
final TextEditingController controller = new TextEditingController();
String result = "";
return widget.id == 3 ? Scaffold(/*version 1*/) : Scaffold(/*version 2*/);
}
Upvotes: 2
Reputation: 51751
The syntax would be:
Widget build(BuildContext context) {
final TextEditingController controller = new TextEditingController();
String result = "";
if (widget.id == 3) {
return Scaffold(/* version 1*/);
} else {
return Scaffold(/* version 2*/);
}
}
id
would be set in the constructor of the StatefulWidget
- and should be final.
Upvotes: 3