3

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(){}?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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*/);
}

Comments

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.