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.