Flutter - Mobile Development - Understanding Dart Data Types
- Posted on March 11, 2024
- Flutter
- By MmantraTech
- 405 Views
In the world of Flutter, DART data types play a significant role in defining the structure and behavior of your user interface elements and managing data within your app. Flutter utilizes DART as its primary programming language, and understanding its data types is essential for building robust and efficient Flutter applications. Let's delve into how these data types are utilized within the Flutter framework.
-SA5rTznwAs.png)
DART, as a versatile programming language, employs various data types to effectively manage and manipulate different kinds of information. Whether you're a beginner or an experienced developer, understanding DART data types is essential for writing efficient and error-free code. In this article, we'll explore the fundamental data types in DART, along with examples to illustrate their usage.
1. Numbers
In DART, numbers can be integers or floating-point values. Integers represent whole numbers without any fractional part, while floating-point numbers include decimal points. DART automatically determines the type of number based on its format.
Example:
int age=30;
double percentage=90.67
2. Strings
Strings are sequences of characters enclosed within single (' ') or double (" ") quotation marks. They are used to represent textual data such as names, messages, or any other form of textual information.
Example:
String name="Techmalasi";
String movie='Captain America";
3. Booleans
Booleans represent logical values, either true or false. They are commonly used in conditional statements and expressions to control the flow of the program.
Example:
bool isRaining = true;
bool isSunny = false;
bool result = 9 > 100;
if(result){}
Lists in DART are ordered collections of objects. They can contain elements of different data types and are mutable, meaning you can add, remove, or modify elements within a list.
Example:
List<int> numbers = [1, 2, 3, 4, 5];
List<String> fruits = ['apple', 'banana', 'orange'];
5.Sets
In Dart, sets are a data structure that stores a collection of unique items. Here's a breakdown of sets in Dart:
- Uniqueness: Sets in Dart can only contain unique elements. If you try to add a duplicate element to a set, it won't be added as the set will ignore it.
- Declaration: You can declare a set in Dart using curly braces
{}
.
For example:Set<int> mySet = {1, 2, 3, 4, 5};
- Creating an Empty Set: You can create an empty set in Dart using the
Set
constructor:Set<String> myEmptySet = Set();
- Adding Elements: You can add elements to a set using the
add
method:mySet.add(6);
- Removing Elements: You can remove elements from a set using the
remove
method:mySet.remove(3);
- Length of Set: You can find the number of elements in a set using the
length
property:print(mySet.length); // Outputs: 5
- Iterating Over a Set: You can iterate over the elements of a set using a loop or the
forEach
method:mySet.forEach((element) { print(element); });
- Operations on Sets: Dart sets support common set operations such as union, intersection, difference, and subset checking. For example:
Set<int> set1 = {1, 2, 3}; Set<int> set2 = {3, 4, 5}; // Union var unionSet = set1.union(set2); // Intersection var intersectionSet = set1.intersection(set2); // Difference var differenceSet = set1.difference(set2); // Subset check bool isSubset = set1.isSubsetOf(set2);
Sets are useful when you need to store a collection of unique items and perform operations that involve checking for uniqueness or comparing collections.
6. Maps
Maps are collections of key-value pairs where each key is unique. They are useful for storing and retrieving data based on specific identifiers or keys.
Example:
Map<String, dynamic> person = { 'name': 'John Doe', 'age': 30, 'isStudent': false };
All Examples in one Umbrella
/*
-Number
-- int
-- double
*/
//int and String are pre built keywords
//Contain one value at a time
int age=56;
//print("age=${age}");
// print('age=${age}');
double percentage=90.56;
String name="Techmalasi";
String movie='Captain America';
//Contcate variables with strings
print("The movie name=${movie}");
//Is Ankit a Topper of the class.
// Yes OR No
bool isTopper=true; //1
bool isRainny=false;//0
print(isTopper);
//Collection in Dart
//What are you list of fav movies?
//Array : collection of something
/*
- Collection
-- List : an Array
-- Set : an Array without duplicate values
-- Map : It is just like JSON DATA - Key value pairs
*/
// Loose Type : Typed not checked
// List movies =["DDLJ",45.78,true,"Captain America","Sholay","Iron Man",30];
//Strict Type : Type checked
// List <String>movies =["DDLJ","Captain America","Sholay","Iron Man"];
// List movies =<String>["DDLJ","Captain America","Sholay","Iron Man"];
//
//
//
// print(movies);
//Duplicates allowed
// List numbers=[3,5,7,3,45,3];
// print(numbers);
// Set numbers ={3,5,7,3,8,3,45.67}; //Map/Set
// print(numbers);
// Set <int>numbers ={3,5,7,3,8,3}; //Map/Set
// print(numbers);
// Set p1 = Set();
// p1.add("5");
//
// print(p1);
//MAP
// Map student={
// "name" :"Techmalasi", //String
// "age" : 40, // int
// "percentage":90.45 // double
//
// };
//
// Map <String,dynamic>student={
// "name" :"Techmalasi", //String
// "age" : 40, // int
// "percentage":90.45 // double
//
// };
// print(student);
//variable type
var age2=45.67;
var name2="kamal";
var movies = ['a',"ddlj"];
//As per input type , Data Tpye is defined
Conclusion
Understanding DART data types is crucial for writing efficient and robust code. By mastering these fundamental data types, you can effectively handle different types of data in your DART programs, leading to better software development practices. Experiment with these data types in your code and explore their functionalities further to enhance your DART programming skills.
Write a Response