Dart Conditional Expressions: Ternary Operator of Java. Dart Flutter Tutorial #4.2

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 มิ.ย. 2018
  • Access 7000+ courses for 15 days FREE: pluralsight.pxf.io/c/1291657/...
    In this beginner tutorial, explore conditional expressions which is similar to what we have as ternary operator in Java. Also explore second type of expression such as "expression1 ?? expression2".
    Next Video: • Dart SWITCH and CASE c...
    Previous Video: • Dart IF ELSE Condition...
    Code Files: bit.ly/2DvFJwZ .
    .
    Please donate and support my work
    (If you think my free tutorials are better than paid ones :)
    - Patreon: bit.ly/patreon-donate
    - Paypal/Payoneer: sriyank123@gmail.com
    - UPI (only for India): smartherd@okaxis
    :: If you want to develop a website or a mobile app, email me your requirement at sriyank.siddhartha@gmail.com :: Free demos provided beforehand ::
    - Access my premium courses: bit.ly/sriyank-courses
    Free Programming courses:
    - Ruby Programming: bit.ly/smyt-r
    - Dart Programming: bit.ly/smyt-d
    - Kotlin Programming: bit.ly/smyt-k
    - Java Programming: bit.ly/smyt-j
    - Kotlin Coroutines: bit.ly/smyt-coru
    Free Flutter course:
    - Flutter App Development: bit.ly/2Rg7EFR
    Free Android courses:
    - Android using Kotlin: bit.ly/smyt-ka
    - Android using Java: bit.ly/smyt-ja
    - Android Material Design: bit.ly/2SMJqU6
    - Android Jetpack Architecture: bit.ly/yt-j
    - Android Multiple Screen Support: bit.ly/smyt-mss
    - Android Retrofit: bit.ly/2Ee6GHn
    More free programming courses:
    - bit.ly/smy-list
    Check out my website:
    - bit.ly/smartherd
    Let's get in touch! [Sriyank Siddhartha]
    LinkedIn: bit.ly/sriyank-linkedin
    Facebook: bit.ly/smartherd-facebook
    Instagram: bit.ly/sriyank-instagram
    Twitter: bit.ly/sriyank-twitter
    Github: bit.ly/smartherd-github
    --- Thank you for your love and support ---

ความคิดเห็น • 41

  • @Nypro6
    @Nypro6 3 ปีที่แล้ว +6

    4:50
    Maybe this would also be a great solution:
    print("${a < b ? a : b} is smaller");

  • @justarandomfishguy8868
    @justarandomfishguy8868 10 หลายเดือนก่อน +6

    You might like me got an error on trying to let the String variable stay null, I found out that after flutter 2.0 which brought in Null safety feature, you need to specify the variable is nullable as follow:
    _String ? name = null_

    • @faheemsameer1598
      @faheemsameer1598 10 หลายเดือนก่อน +3

      thank you, faced the issue and your answer came out first

    • @emmanuelmushi3072
      @emmanuelmushi3072 7 หลายเดือนก่อน +1

      Thank you very much.

    • @smoke_ffx.
      @smoke_ffx. 24 วันที่ผ่านมา +1

      Thanks

  • @saisasisai
    @saisasisai 4 ปีที่แล้ว +2

    Nice 👌 Tutorial series ...!

  • @mohanbarman5982
    @mohanbarman5982 5 ปีที่แล้ว +2

    really thanks for your tutorials ;-)

  • @mohanbarman5982
    @mohanbarman5982 5 ปีที่แล้ว +8

    print("${a>b ? b : a} is smaller than ${a

  • @krishnakumarramachandran5888
    @krishnakumarramachandran5888 5 ปีที่แล้ว +4

    Good Explanation sir👌

  • @dineshbhavsar1435
    @dineshbhavsar1435 3 ปีที่แล้ว +10

    Sir It Show Me Error: A value of type 'Null' can't be assigned to a variable of type 'String'.

    • @afaqueali9706
      @afaqueali9706 2 ปีที่แล้ว +7

      Yes, because this video is old and now dart has included null safety in its latest version. Try using 'var' instead of 'String' you will see no error .

    • @NusratJahan-pg1mi
      @NusratJahan-pg1mi ปีที่แล้ว

      @@afaqueali9706 nice it's work . take love

    • @emmanuelmushi3072
      @emmanuelmushi3072 7 หลายเดือนก่อน

      You can try this also: String ? name = null
      It worked for me.

    • @LolLol-br6zj
      @LolLol-br6zj 5 หลายเดือนก่อน

      to wite the null value in variable use the syntax
      String? "Variable name" = null;
      if you have any confusion please find the example below
      void main() {
      //String name = "Ak";
      String? name = null; // Use nullable type String?
      String theName = name ?? "PK";
      print(theName); // This will print "PK" because name is null
      }

  • @thebongtwister
    @thebongtwister 3 ปีที่แล้ว

    At 5.38, why you not using " start and close inside print ?

  • @karthickrajalearn
    @karthickrajalearn 4 ปีที่แล้ว +1

    In 4m 49 sec
    It is Very useful

  • @faisalfotoh1909
    @faisalfotoh1909 2 ปีที่แล้ว

    unlocked: i

  • @SyedAlirazarizvi72
    @SyedAlirazarizvi72 3 ปีที่แล้ว

    Hi,
    I am trying to type syntax same as you for testing the second method of conditional expressions exp1 ?? exp2.
    String name = "Allan";
    String nameToPrint = name ?? "Guest User";
    print(nameToPrint);
    On the first expression name the program is showing an error "Condition must have a static type of 'bool'
    In the output window the error pointer is pointing on second question mark, also the red mark is pointing on second question mark in the code.
    Can you please help me finding out the actual error?
    Thanks for presenting such a helpful series of videos.

    • @renziz9541
      @renziz9541 2 ปีที่แล้ว

      that's weird, I just tested your code and it worked fine for me. are you using the DartPad compiler?

    • @afaqueali9706
      @afaqueali9706 2 ปีที่แล้ว

      Yes, because this video is old and now dart has included null safety in its latest version. Try using 'var' instead of 'String' you will see no error .
      try this code.
      var name = null;
      String nameToPrint = name ?? "Guest User";
      print(nameToPrint);

    • @AbhayKumar-lf3ct
      @AbhayKumar-lf3ct ปีที่แล้ว

      @@afaqueali9706 but how to print name as "tom"

  • @chiragbansal5060
    @chiragbansal5060 3 ปีที่แล้ว

    Please tell me how to take input from the user.

    • @ritikmitra7802
      @ritikmitra7802 3 ปีที่แล้ว

      it is a big procedure but you can take input from user by using if and readsync some code is not working because now the latest version is DART 2 and he is explaining dart 1 thats why

  • @bscoeryan
    @bscoeryan ปีที่แล้ว

    Can have the first video if else..

  • @imgeekyt
    @imgeekyt 10 หลายเดือนก่อน

    String name=null;
    got error on above line
    The value 'null' can't be assigned to a variable of type 'String' because 'String' is not nullable.

    • @LolLol-br6zj
      @LolLol-br6zj 5 หลายเดือนก่อน +1

      to wite the null value in variable use the syntax
      String? "Variable name" = null;
      if you have any confusion please find the example below
      void main() {
      //String name = "Ak";
      String? name = null; // Use nullable type String?
      String theName = name ?? "PK";
      print(theName); // This will print "PK" because name is null
      }

  • @ritikmitra7802
    @ritikmitra7802 3 ปีที่แล้ว +1

    String name = null; is not working ! but String? name = null; is working explain ?

    • @emmanuelmushi3072
      @emmanuelmushi3072 7 หลายเดือนก่อน

      Instead of using String name=null, use this code:
      String ? name = null;
      You can also use:
      var name = null;

  • @wajdibac3236
    @wajdibac3236 ปีที่แล้ว +1

    now you can't assign String name = null .. It should be String? name = null; or it will not work .

    • @corasfm45
      @corasfm45 ปีที่แล้ว

      and how to name it tio? it still give warning operand in line code String nameToPrint = name ?? "Guest User" has type String which excluded null

    • @wajdibac3236
      @wajdibac3236 ปีที่แล้ว +1

      @@corasfm45 this code works fine in visual code :
      void main() {
      // Conditional Expressions
      // 1. condition ? exp1 : exp2
      // If condition is true, evaluates expr1 (and returns its value);
      // otherwise, evaluates and returns the value of expr2.
      int a = 5;
      int b = 7;
      if (a < b) {
      print('$a is smaller');
      } else {
      print('$b is smaller');
      }
      //we can do it instead like this
      a < b ? print('$a is smaller') : print('$b is smaller');
      // or like this
      int smallerNumber = a < b ? a : b;
      print('$smallerNumber is smaller ');
      // 2. exp1 ?? exp2
      // If expr1 is non-null, returns its value; otherwise, evaluates and
      // returns the value of expr2.
      String? name = null;
      String nameToPrint = name ?? "Guest User";
      print(nameToPrint);
      }

    • @corasfm45
      @corasfm45 ปีที่แล้ว +1

      @@wajdibac3236 i use visual code too and dart extension latest v3.56.0. i mean if i want the output like name = tio. example code
      String? name = null;
      name = "Tio";
      String nameToPrint = name ?? "Guest User";
      print(nameToPrint);
      it give me error warning like my previous question but the output is right it print Tio. and is there a way to get rid that warning error?

    • @wajdibac3236
      @wajdibac3236 ปีที่แล้ว

      @@corasfm45 Sorry I just read the error in your comment. that's related to dart version don't worry about it. It is not an error, if you check below that error you will find that your code is actually get executed successfully and you can see the result ...

    • @sanskaarz
      @sanskaarz ปีที่แล้ว

      @@wajdibac3236 👍

  • @worldofrup9085
    @worldofrup9085 4 ปีที่แล้ว

    This is ternary operator

  • @rockstreethomie
    @rockstreethomie 2 ปีที่แล้ว +1

    String is not nullable

    • @nikhiljohnjose6739
      @nikhiljohnjose6739 2 ปีที่แล้ว +1

      Yup. Try using String? instead of String
      String? name = null should work.

    • @rockstreethomie
      @rockstreethomie 2 ปีที่แล้ว +1

      @@nikhiljohnjose6739 It worked.Thanks Nikhil Bro

  • @snehagsneha4542
    @snehagsneha4542 5 ปีที่แล้ว

    video not clearly visible

    • @adii18s
      @adii18s 4 ปีที่แล้ว +2

      your download quality is low make it high in settings