How ro use Ternary opertor for multiple cases ?
Flutter Tutorial:
Flutter Widgets:
Flutter Advance
Flutter REST API
Advanced Concepts
Wrap vs Builder vs OverBarFlow
Circular progress contain Icon
Flutter State management Comparison
Flutter Database
Flutter Token Expired Handling
Flutter Provider
Flutter GetX
Flutter with Native
Flutter Tips:
Interview Questions
Flutter 100 Interview Questions
In Flutter, when you have a small number of options to check (more than two), you can use a series of ternary operators to handle different cases in a concise way. Each ternary operator checks a condition and returns a value based on whether the condition is true or false.
Multiple Ternary Example
String getFeedback(int rating) {
return rating == 5
? 'Excellent'
: rating == 4
? 'Good'
: rating == 3
? 'Average'
: 'Poor';
}
In this code: If rating is 5, it returns ‘Excellent’. If rating is 4, it returns ‘Good’. If rating is 3, it returns ‘Average’. Otherwise, it returns ‘Poor’.
Multiple Ternary in Widgets Example
Text (rating == 5
? 'Excellent'
: rating == 4
? 'Good'
: rating == 3
? 'Average'
: 'Poor')
You can use this approach wherever you want in Flutter. It helps make your code shorter and handle different situations or conditions effectively.