The Dart language has special support for the following types:
Use int
and double
to represent number.
int num1 = 50; double num2 = 7.88756778;
Both int
and double
are subclass of the class num
. This num
class contains the definition of common mathematical operators like +
, -
, *
and /
.
You will also find some common methods like abs()
, floor()
etc.
var x = 1; // Store a normal integer number var hex = 0xDEADBEEF; // Store a hexadecimal number var y = 1.1; // Store a double number var exponents = 1.42e5; // Store a double number in scientific notation
If your expression contains both int
and double
value. The int
value will automatically be converted to double
and then the further operation will be carried out. For example,
double z = 1; // Equivalent to double z = 1.0.
The following example shows how you can convert a string to number:
// String -> int var value = int.parse('100'); assert(value == 100); // String -> double var value = double.parse('1.1'); assert(value == 1.1); // int -> String String value = 15.toString(); assert(value == '15'); // double -> String String valueOfPi = 3.14159.toStringAsFixed(2); assert(valueOfPi == '3.14');
A Dart string is a sequence of UTF-16 code units. You can use either single or double quotes to create a string:
var s1 = 'Single quotes work well for string literals.'; var s2 = "Double quotes work just as well."; var s3 = 'It\'s easy to escape the string delimiter.'; var s4 = "It's even easier to use the other delimiter.";
You can also use String
keyword to create a String.
String str = "It's even easier to use the other delimiter.";
You can put a value of a variable or identifier within a string. Use $variableName
within the string.
var name = "Santanu Bera";
var str = "My name is : $name";
Not just that, you can also use complex complex or call a method that returns a value. You just need to put the expression within the curly bracket just like the following syntax -
var name = "My name is : ${getName()}. And I am ${person.getAge()} years old !".
Within the curly bracket you can throw any complex logic. This is an awesome feature.
The ==
operator is used to check two string if they are equals or not. It returns true
if both string are equal otherwise it returns false
.
The +
operator is used to concate two string.
Whenever you are adding string with a number, Dart automatically calls toString()
method of the inbuilt num
class. This methods is predefined and thats why we don't need to define it over and over again whenever we have to use numbers. But what if we want to concatenate our custom object with a string. In these case, we need to define a toString()
methods to our custom class. So whenever we add a object with a string, Dart will automatically call the toString()
method of the corresponding class.
var pet1 = "Cat"; var pet2 = "Dog"; var pet3 = "Cat"; var totalPets = 3; pet1 == pet2 // false pet1 == pet3 // true // Example of Concatenation str = "I have " + totalPets + " pets";
Another interesting thing about Dart when using String. You can concate strings without using any operator or methods. Just put the string blocks one after another. The compiler will automatically detect them and concate them automatically. But remember, there should be nothing excepts white spaces between two blocks of string.
var s1 = 'String ' 'concatenation' " works even over line breaks."; assert(s1 == 'String concatenation works even over ' 'line breaks.'); var str = "hi" " buddy !"""; print(str); // hi buddy !
Another way to create multiline string using a tripple quote.
var s1 = ''' You can create multi-line strings like this one. '''; var s2 = """This is also a multi-line string.""";
You can create a “raw” string by prefixing it with r
:
var s = r'In a raw string, not even \n gets special treatment.';
Anything inside the Raw String doesn't get escaped. The are displayed and processed as how they are stored, no internal conversion takes place when using Raw String.
To check if a string is empty or not use isEmpty
property of the string objct.
var str = ""; if(str.isEmpty){ print("String is Empty"); }else{ print("String is not Empty"); }
To represent boolean values, Dart has a type named bool
. Only two objects have type bool: the boolean literals true
and false
, which are both compile-time constants.
Dart’s type safety means that you can’t use code like if (nonbooleanValue)
or assert (nonbooleanValue)
. Instead, explicitly check for values, like this:
// Check for an empty string. var fullName = ''; assert(fullName.isEmpty); // Check for zero. var hitPoints = 0; assert(hitPoints <= 0); // Check for null. var unicorn; assert(unicorn == null); // Check for NaN. var iMeantToDoThis = 0 / 0; assert(iMeantToDoThis.isNaN);
Lists are discussed in Lists
chapter.
Sets are discussed in Sets
chapter.
Maps are discussed in Maps
chapter.
We will discussed about Runes and symbols later.