4

I wanted to learn how can I apply "if" condition in my flutter code? As I am new to dart language coding.

Suppose in a code like below, i want to add condition that if the counter's value is 1 then "You have pushed the button $_counter time" else "You have pushed the button $_counter times"

children: <Widget>[

        new Text(
          'You have pushed the button $_counter times:',
        )/*,
        new Text(
          '$_counter',
          style: Theme.of(context).textTheme.display1,
        ),*/
]

P.S. its just a simple example for me to understand how to use if condition in flutter.

5
  • 1
    What code, what condition? Please provide a concrete example. Commented Jul 10, 2018 at 6:48
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. Commented Jul 10, 2018 at 6:56
  • All i need is the syntax of "if" condition in flutter. Commented Jul 10, 2018 at 7:09
  • There are several ways to do it depending on the situation, so one or more concrete examples are required. Commented Jul 10, 2018 at 7:19
  • I recommend reading some more about the Dart language before asking specific questions. Start with dartlang.org/guides/language Commented Jul 10, 2018 at 7:55

3 Answers 3

5

For such simple cases you can use the ternary if ?: inside string interpolation:

    new Text(
      'You have pushed the button $_counter time${_counter != 1 ? 's' : ''}:',
    )
Sign up to request clarification or add additional context in comments.

4 Comments

Multiple errors like expected a class member, expected to find ';',etc, and i don't know want to add and what to exclude!
Sorry, I looked through the code 10 times after your first comment but didn't see it. I had a : instead of ?. I updated my answer.
Dont be sorry, already my reputation is decreasing due to ambiguous question! And ya even after changes,the error remain.
I would need more information. I can't imagine this to be related to the code I posted. Can you add a screenshot to your question with the errors and the code that causes them?
1

for the case of the conditions you have just a single condition then you can use the basic ternary operators,

child: Text(
     '{fee=="FREE"?"Free":fee}',
     ),

But if you have multiple conditions or value that need to compare from the index position(i.e. you have to fetch value and then need to put in condition) in listview then you can add method as your text value in your Widget as follows:

child: Text(
  '${checkForPrice(index)}',
  ),
),

checkForPrice(int index) {
String strFee = data['payload']['webinar'][index]['fee'];
String finalFee = "";
if (strFee == "FREE") {
  finalFee = 'FREE';
} else {
  finalFee = '\$ ${data['payload']['webinar'][index]['fee']}';
}

return finalFee;}

This will definitely help you in showing data with multiple conditions.

Comments

-2

at the first time i tried flutter i have no idea what dart language is but after some browsing i found this useful documentation. You can find anything you looking for right there including if statement that you are asking about. But i can tell you the if statement works the same as any other language such as JavaScript, Java, etc.

Edit: here i give you some example how to use it in flutter

Future<Null> toTheHomeScreen() async {
await _signInWithGoogle();
SharedPreferences sprefs = await SharedPreferences.getInstance();
if (sprefs.getString('idToken').isNotEmpty) {
  new Future.delayed(new Duration(seconds: 3), () {
    new CircularProgressIndicator();
    Navigator.of(context).push(
      new MaterialPageRoute(
        builder: (BuildContext context) {
          return new HomeScreen();
        }
      )
    );
  });
} else {
  Navigator.of(context).pop();
}

}

the if statement check if there is any key named 'idToken' stored in sharedpref, if it not empty user will redirect to home page.

I hope this will help you.

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.