Default Value & Type Conversion in Dart

Default Value & Type Conversion in Dart

ยท

2 min read

  • As we know in dart there are total 4 data type which is int ,String, double and bool. in where bool in only use for True and False.

  • But other three data type can use interchange , but there is catch that we can not one data type another in same object.

  • So, Using this interchange data type with together we can use conversion in dart.

  • There are 4 Conversion that we can use

    1. String to Int

    2. String to double

    3. Int to String

    4. double to String

      • We will see this four conversion with example one by one
        // String to Int

        void main()
        {
        int a =10;
        var b = int.parse("20");
        print(a+b); //30

        }
  • Here , we declare a = 10 with int data type and var for b but we use " int.parse() " with b variable to change data type into int.

  • and this same work for double , we have to just change int.parse() to "double.parse()".

        //String to double 
        void main()
        {
        int a =10;
        int b = double.parse("0.20");
        print(a+b); //10.20

        }
  • For changing data type int/ double to String we just have to use .toString() method.

  • here , we can see that in a variable we have declare value as 10 which in int type so we have use .toString() method to convert into string and we can also check data type with " .runtimeType "

      void main()
      {
      String a = 10.toString();
      print(a); //10
      print(a.runtimeType); //String
      }
    

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย