Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying widget conditionally in flutter

Tags:

flutter

dart

I am just writing a simple application in flutter and I came across an situation where I need to display widget conditionally. When I use the ternary operator it is working perfectly fine.

Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("I quiz you, OK?"),
        ),
        body: (_questionIndex < _questionList.length)
            ? Quiz(
                questionList: _questionList,
                questionIndex: _questionIndex,
                answerSelected: _answerSelected)
            : Result(_finalScore, _reset),
      ),
    );
  }
//Woks fine

but when I replace it with if-else block I am getting an error as

Expected an identifier.dart(missing_identifier)

Expected to find ')'.dart(expected_token)

Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("I quiz you, OK?"),
        ),
        body: if(_questionIndex < _questionList.length)
            { 
              Quiz(
                questionList: _questionList,
                questionIndex: _questionIndex,
                answerSelected: _answerSelected)
            }
            
            else
            { 
              Result(_finalScore, _reset)
            },
      ),
    );
  }
//Getting error

What could be the issue as all brackets are balanced perfectly. I know I could just use ternary operator but there might arise a situation where if-else ladder would be required.

Note: Quiz() and Result() are custom widgets.

like image 298
endangered species Avatar asked Oct 26 '25 05:10

endangered species


2 Answers

You cannot use a if/else statement as you did, here are several ways to do it:

body: () {
  if (_questionIndex < _questionList.length) {
    return Quiz(/* ... */);
  } else {
    return Result(/* ... */);
  }
}(),

or

body: Builder(
  builder: (context) {
    if (_questionIndex < _questionList.length) {
      return Quiz(/* ... */);
    } else {
      return Result(/* ... */);
    }
  },
),
like image 190
Valentin Vignal Avatar answered Oct 27 '25 21:10

Valentin Vignal


you can create a method returning a widget and easily return what ever you want.

  Widget func(){ 
if(_questionIndex < _questionList.length){
 return Quiz( questionList: _questionList, questionIndex: _questionIndex, answerSelected: _answerSelected);}    

    else if(_questionIndex >= _questionList.length){ 
            return  Result(_finalScore, _reset); 
            }
    else{
    //widget is must
    return Container();
    }}
            Widget build(BuildContext context) {
                return MaterialApp(
                  home: Scaffold(
                    appBar: AppBar(
                      title: Text("I quiz you, OK?"),
                    ),
                    body: func();
              }
like image 21
Osama Kashif Avatar answered Oct 27 '25 19:10

Osama Kashif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!