Dart Basics -Flutter #2

incrypto32

Krishnanand

Posted on March 30, 2020

Dart Basics -Flutter #2

In the previous article in this series, we have gone through the basic syntax of Dart.
Cool Is'nt it ? told ya.. Dart is a simple and easy to learn programming language for anyone with basic programming knowledge.
This article is another quick run through dart basics and mainly deals with the different datatypes available in dart.

Datatypes

String

Strings are sequences of characters which we use to represent text.We can use single,double or triple quotes to represent strings in dart

void main() { 
   String name1 = 'Sachin';
   String name2 = "Virat Kohli";
   String name3 = '''MS Dhoni''';
   print('$name1,$name2,$name3');
}
Enter fullscreen mode Exit fullscreen mode

We can join two strings easily with the help of the + operator.

void main() { 
  String firstName = 'Sachin';
  String lastName = 'Tendulkar';
  String fullName = firstName +' ' + lastName;
  print(fullName);
}
Enter fullscreen mode Exit fullscreen mode

Dart also has many builtin methods within the String class which simplifies certain string operations(eg:toUpper()) As this is a quick intro to datatypes in dart these concepts are out of the cope of this article.

Numbers

Numbers are mainly represented in dart as int or double.int Stands for integer.
double is the keyword used for 64bit floating point numbers.
Here is how we declare numbers in dart.

int num1 = 10;
double num2 = 10.5;

Enter fullscreen mode Exit fullscreen mode

We will come across instances when we have to convert String into num.
We can do that.

void main() { 
  String num1='10';
  int num2=num.parse(num1);
  print(num2);
}
Enter fullscreen mode Exit fullscreen mode

List

We all know about arrays.Lists are similar to arrays they are simply an ordered group of objects.
The easiest way for declaring a list is using the assignment operator to assign a variable to an empty list.
Check out the example below

   var emptyList = [];
   var snackList = [ 'Burger' , 'Sandwich' ];
Enter fullscreen mode Exit fullscreen mode

We can also explicitly explicitly specify whether a variable is a list or not so that the variable can only be assigned to List type.
for example :

   List nameList = [ 'Rahul' , 'Goutham' , 'Nirmal' , 'Shivani' ];
Enter fullscreen mode Exit fullscreen mode

If we want the list to be a list of String we can also explicitly specify that within '<>'.
Checkout this code snippet

   List<String> nameList = [ 'Rahul' , 'Goutham' , 'Nirmal' , 'Shivani' ];
   List<num> numberList =[1,2,4];
Enter fullscreen mode Exit fullscreen mode

Apart from these above techniques if we want to declare a List with a specific size we can use : List(listsize)

var newList = List(3);
Enter fullscreen mode Exit fullscreen mode
Accessing List Items

Checkout this list of fruits.

List<String> fruitList = ['Apple' , 'Orange' , 'Banana' , 'Strawberry'];
Enter fullscreen mode Exit fullscreen mode

We can use listname[index] to access a particular item in a list
fruitList[0] is 'Apple'.
We can also use it to modify an item in the list.

void main() { 
  List<String> fruitList = ['Apple' , 'Orange' , 'Banana' , 'Strawberry'];
  fruitList[0] = 'Pineapple';
  print(fruitList);
}
Enter fullscreen mode Exit fullscreen mode

The output will be : [Pineapple, Orange, Banana, Strawberry]

Adding Items to a List

We can use the List.add() method to add an item to end of the List or List.insert( index, element) to add an item to a particular index.

void main() { 
  List<String> fruitList = ['Apple' , 'Orange' , 'Banana' , 'Strawberry'];
  fruitList.add('Grapes');
  print(fruitList);
  fruitList.insert(2, 'Mango');
  print(fruitList);
}

Enter fullscreen mode Exit fullscreen mode

There are a lot of properties and methods within the List class that will be of great use when we start building flutter apps.
Do check out the official documentation for more info.

Map

Map is also a type of collection in which we can store data in the form of key-value pairs.If you are coming from a python background then you can think of Maps as dictionaries in python.
For example, consider a case where we have to store data of a phone. It will contain different details like the Brand name, Model Name, Price, etc.
We can easily store these details in a map.

var phone1={
    'Brand': 'Samsung',
    'Model' : 'Galaxy S10',
    'Price' : 30000,
  };
Enter fullscreen mode Exit fullscreen mode
Accessing and Modifying data in a Map

We can access a particualar value using Map['key_name'] and also modify a field using the same.

void main() { 
  var phone1={
    'Brand': 'Samsung',
    'Model' : 'Galaxy S10',
    'price' : 30000,
  };
  phone1['price'] =35000; //modifying a field in a map
  print('${phone1['Brand']} ,${phone1['price']}');
}
Enter fullscreen mode Exit fullscreen mode
Output: Samsung ,35000
Enter fullscreen mode Exit fullscreen mode

So that's a very quick intro to datatypes in dart.
Stay tuned for more content..

💖 💪 🙅 🚩
incrypto32
Krishnanand

Posted on March 30, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related