Data Types In Dart

What are datatypes?

In the Dart programming language, a data type is a classification of values that determines the possible values that a variable can hold, and the operations that can be performed on those values.

Dart has a number of built-in data types, including:
int: An integer value, such as 1, 2, or 3.
double: A floating-point value, such as 1.5 or 2.7.
String: A sequence of characters, such as “hello” or “world”.
bool: A Boolean value, either true or false.
List: An ordered collection of values, such as [1, 2, 3].
Set: An unordered collection of unique values, such as {1, 2, 3}.
Map: A collection of key-value pairs, such as {“key”: “value”}.

void main() {
  int intType = 10;
  String strType = "String Type";
  bool boolType = true;
  List<String> listType = ["data 1", "data 2"];
  Map<String, String> mapType = {'name': 'Nandan', 'info': 'software engineer'};
  Set<String> setType = {'data', 'data'};

  print(intType);
  print(strType);
  print(boolType);
  print(listType);
  print(mapType);
  print(setType);

  /*
  10
  String Type
  true
  [data 1, data 2]
  {name: Nandan, info: software engineer}
  {data}
  */
}

Dart is also a dynamically-typed language, which means that the type of a variable is determined at runtime rather than at compile-time. This means that you can use the var keyword to declare a variable and its type will be inferred from the value assigned to it.

var name = "Curious Developers Community";

Keypoints

1. We will check rounding double value into number of decimal places
2. We will check adding multi line string

Round Double Values

The .toStringAsFixed(2) is used to round the double value up-to 2 decimal places in dart. You can round to any decimal places by entering number like 2, 3, 4.

void main() {
  double data = 2.0000021324;

  //procedure to round to respective decimal point
  print(data.toStringAsFixed(2));
}

  /*
	2.00
  */

Multi Line String In Dart

if we want to use multiline string we need to us like this which is mention in brackets (‘’’multi line string here’’’)/(”””multi line string here”””)

void main() {
  String info_1 = '''
  This is Multi Line Text
  With 3 dots quote
''';

  String info_2 = """
  This is Multi Line Text
  With 3 dots quote
""";

  print(info_1);
  print(info_2);
}

/*
 This is Multi Line Text
  With 3 dots quote

  This is Multi Line Text
  With 3 dots quote
*/

Type Conversion

In the Dart programming language, you can convert the value of one data type to another using type conversion functions.

//string to an integer
int.parse(string)


//string to a floating-point number.
double.parse(string)

//Converts a string representation of a boolean value to a Boolean.
bool.fromEnvironment(name, defaultValue)


//Converts any value to a string.
string.toString()

String to int

we can convert String to int using int.parse(string) method

void main() {
  var str = '1999';
  var num = int.parse(str);
  print(num); 
}

// Output: 1999

String to a floating-point number.

we can convert String to double using double.parse(string) method

void main() {
  var str = '10.029';
  var num = double.parse(str);
  print(num);
}

 // Output: 10.029

String representation of a boolean value.

we can convert string representation of a boolean value to a boolean using bool.fromEnvironment(name, defaultValue).

void main() {
    var str = 'false';
    var boolVal = bool.fromEnvironment(str);
    print(boolVal); 
}

 // Output: false

More on List

In Dart, a List is an ordered collection of Similar Type Elements, similar to an array in other programming languages. You can think of a list as a collection of values of the same type, stored in a specific order

Lists are dynamic in size, meaning that you can add or remove elements from a list at any time. Here is the way of creating list in a Dart and Note – List always starts from index 0

List<int> numbers = [1, 2, 3, 4, 5];

More on Set

In Dart, a Set is a collection of unique elements, meaning that no element can be repeated in a set. Sets are unordered, meaning that the elements in a set do not have a specific position or index. Sets are similar to lists, but with the constraint that all elements must be unique.

Here is an example of how you can create a set in Dart:

Set<String> names = {'John', 'Jane', 'Jim'};

More On Maps

In Dart, a Map is a collection of key-value pairs, where each key is associated with a single value. Maps are similar to dictionaries in other programming languages, and they allow you to store values in an unordered collection, where each value is retrieved by its key.

Here is an example of how you can create a map in Dart:

Map<String, int> scores = {'John': 50, 'Jane': 60, 'Jim': 70};

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top