In this blog we will learn about data types for Flutter in Dart. Data types are fundamental building blocks for managing and manipulating data.
Introduction
Dart, the programming language used for Flutter development, and it support a set of data types that help developers manage data efficiently.
Data types are essential for handling different kinds of information. Knowing how to use these data types helps you manage data effectively when building apps.
Type of Data type
There are multiple type of data type in flutter which is mention below.
1. Numeric Data Type:
Dart provides two primary numeric types to handle numbers: int
and double
. Each serves different purposes and has its own characteristics.
- int:-> Represents integer values, which are whole numbers without any decimal point.
- double:->Represents floating-point numbers, which can contain decimal points.
1 2 3 4 5 6 7 8 |
// Int int age = 30; int temperature = -5; //Double double pi = 3.14; double temperature = -10.5; |
2. String Data Type:
In Dart, strings are used to represent sequences of characters, such as text. They are a fundamental data type and are widely used in applications for displaying messages, processing user input, and handling textual data.
1 |
String greeting = 'Hello, Webkul'; |
3. Booleans Data Type:
In Dart, the boolean data type is represented by the bool
keyword and is used to store truth values: either true or false.
1 2 |
bool isActive = true; bool hasPermission = false; |
4. Lists Data Type:
Lists in Dart are an ordered collection of items, allowing you to store multiple values in a single variable. They are flexible, versatile, and a fundamental data structure in Dart programming.
1 2 |
List<int> numbers = [1, 2, 3, 4, 5]; List<String> fruits = ['apple', 'banana', 'cherry']; |
You can create a list using square brackets ([]
). Lists can hold items of any data type, including integers, strings, objects, etc.
5. Sets Data Type:
A Set in Dart is an unordered collection of unique items. It is used when you want to store values without duplicates and do not care about the order of the elements.
You can create a Set
using curly braces {}
or the Set
constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
var mySet = {1, 2, 3}; //Using the Set Constructor: var mySet = Set<int>(); //Add and element mySet.add(4); //Remove element mySet.remove(2); //Print Over All Set for (var item in mySet) { print(item); } Output 1,3,4 |
6. Map Data Type:
In Dart, a Map
is a collection of key-value pairs, where each key is unique. Maps are useful for storing and retrieving data based on a specific key.
1 2 3 4 5 6 7 8 9 10 |
var myMap = {'name': 'Suraj', 'age': '24'}; myMap.forEach((key, value) { print('$key: $value'); }); Output name: Suraj age: 24 |
Conclusion
In this blog we have discussed about Data Type for Flutter in Dart.
I hope this blog is helpful to UnderStand this topic, you can visit here for more knowledge about datatype.
You can also check other blogs from here for more knowledge.