Flutter : Dart Data Types | List data type | Map | Flutter tutorial for beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the in data type is used to represent whole numbers integers such as Min -1 0 42 and 1,000 let's work with int variables declare an INT variable called apple and set it equal to five similarly declare another variable named banana and set it equal to three the ink data type is specifically used to declare whole number numers like these now let's print the value of the Apple variable to display this value within a string we use the dollar sign dollar symbol like so I have dollar Apple apples this results in the output I have five apples we can perform similar operations with the banana variable using an integer variable we can perform various arithmetic operations including addition subtraction and multiplication among others let's calculate the total number of fruits by declaring a total fruits variable and setting it equal to Apples plus bananas this calculation gives us a total of eight fruits display the total fruits variable to see the result yes a total of eight fruits as you can see the in variable allows us to handle various operations involving whole numbers effectively the double data type is used to represent decimal numbers floating points numbers like -1.5 3.14 and 0.001 to work with the double data type follow these steps declare a double variable using the double keyword let's name it radius and set it to 5.0 declare another variable named Pi calculate the area of the circle since the result is a decimal value declare a double variable for the area calculate the area using the formula area equal Pi into radius square print the area variable display it as the area of the circle with radius dollar radius is dollar area of the circle this output is generated for this type of calculation which involves decimal data types this output is generated for this type of calculation which involves decimal data types work with the string data type follow these steps declare a string variable using the string keyword let's name it name and set it to Brian print the value of the name variable using string interpolation declare another string variable named message in this step we concatenate two strings hello and the name variable print the message variable to see the result it should show hello Brian these types of string related coding examples demonstrate the use of the string data type in Dart the bull data type represents Boolean values which can be either true or false to work with the bull data type follow these steps declare a Boolean variable using The Bull keyword let's name the variable is sunny and set it to true check the condition using an if statement if the value is true enter the code block and print the message it's a sunny day if the condition is false print it's not a sunny day when you run the code with the variable set to true it displays it's a sunny day change the variable to false and the code displays it's not a sunny day this example demonstrates how the bull variable is used for these types of operations lists are used to store ordered collections of items which can be of any data type lists can grow or Shrink in size dynamically to work with lists follow these steps declare a list very variable you specify the data type included in the list variable using angle brackets in this case we're including string data let's name the variable fruits and enclose the data in square brackets separated by commas for example you can enter fruit names like apple banana and orange this list contains three items print the variable fruits to display the list items each item in the list has an index starting with zero to display the zero item use the variable followed by the index number in square brackets for instance to display the first item use fruits zero to get apple if you change the index to one it displays the second item which is banana changing it to two displays the third fruit Orange it's also possible to assign a new value to a particular index for example you can set fruits 1 equals grape which changes the second item to Grape print fruits to see the updated list the add function allows you to add a new item to the list for instance you can use fruits. add strawberry to add strawberry as the fourth item when you print the list it shows all four items it's possible to insert an item at a particular position using the insert function specify the index position as the first parameter and add the value as the second parameter for example fruits. insert zero strawberry inserts strawberry in the first position print the list to see five items with strawberry in the first position to remove items from the list you can use the do remove remove function in this example we're removing banana using the remove function when you print the list banana is gone you can also remove an item from a specific index using the remove at function for instance fruits. removeat 2 removes the third item Orange after executing it the list no longer contains orange that. length property gives you the number of items in the list fruits. length in this case shows that there are three items in the list these are some important concepts related to working with lists maps are used to store key value pairs where keys and values can be of different data types maps are similar to lists but each item in a map consists of a key value pair to declare a map use angle brackets to specify the data types of keys and values declare a map variable such as student grades Define the data within curly brackets each entry has a key a string in this case and a value an integer separated by a colon entries are separated by commas in this example the map student grades contains data for three people you can print these values using the print function to access the value associated with a specific key use the variable name followed by the key in square brackets for example to access Bob's grade use student grades Bob which displays 89 to add a new entry assign a key value pair to a variable like key equals value for instance to add David with a grade of 87 use student grades David equals 87 after this display that student grades map to see the updated data to check if a specific key exists in the map use this contains key function if the key is present it returns true for example student grades. contains key Charlie returns true while student grades do contains key Brian returns false indicating that Brian is not in the map to remove an entry use the remove function and provide the key as a parameter for example to remove Bob use student grades. remove Bob afterward Bob will be removed from the map to find the count of items in the the map use the length property in this case student grades. length displays a count of three this explanation provides details about working with maps in Dart the dynamic data type is used when the data type of a variable is not known at compile time or can change at runtime however use it with caution as it bypasses type checks when you declare a dynamic variable it is possible to change its data type at runtime let's create a dynamic variable named DV here are the steps first declare an integer variable and print it to see its value next assign a string value to the DV variable this change is also displayed then assign a double value to DV the variable now holds a double data type you can even assign a Boolean value to DV and it works without any problems this flexibility to change data types at runtime is the special characteristic of dynamic variables the VAR keyword allows Dart to infer the data type based on the assigned value it is often used when the data type is evident from the initialization here's how it works declare a VAR variable such as name assign it a string variable from this point on it is considered a string variable and you can't change its data type unlike Dynamic variables where you can change the data type at any time with VAR it's not possible to assign another data type attempting to assign an integer value for example will result in an error this distinction between VAR and dynamic is important when choosing which keyword to use for your variables the object data type is the root type for all dart objects it is rarely used directly but can be used when you need a variable that can hold any type of object here's how it works create an object variable named assign it a string value such as hello Dart when you print the object it is displayed declare another variable and assign an integer value to it attempt to ass assign the integer value directly to the object one variable this results in an error because it is not possible to assign it directly first check if the ob one variable contains an integer value using is in then convert the object's value to an integer using as int now it works without errors when you need to convert an object's value to a string you can use to string in the example declaring a double value in the object and then assigning it to a string variable initially results in an error in the example declaring a double value in the object and then assigning it to a string variable initially results in an error however after using the to string method it works as expected this demonstrates the flexibility of the object data type in Dart allowing it to hold values of different types the null data type represents the absence of a value it is often used to indicate that a variable or object has no meaningful value here's how it works first declare a string variable without assigning any value by default it is null when you try to print a non-nullable variable you get an error because it's expected to have a value to make a variable nullable add a question mark after the data type makes it nullable and you can assign it the value null without errors this Behavior applies to variables of any data type if you declare an invariable it will also be non-nullable by default adding a makes it nullable and prevents errors you can use the double question mark to check if a variable is null or not if it is not null you can assign another value for example string name equals string final name equals name unknown will assign unknown to final name if name is null similarly you can check an in variable for null and assign a default value if you assign values to the variables before checking they will display their assigned values if they are null they will display the default values you provided during the check this is how the null data type is used in Dart to handle variables that may or may not have a value you
Info
Channel: True Coders
Views: 48
Rating: undefined out of 5
Keywords: dart datatypes, map dart, dart map, flutter map, dart list, flutter list, details about flutter data types
Id: jvuXc4R4jdU
Channel Id: undefined
Length: 21min 0sec (1260 seconds)
Published: Thu May 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.