The Basics Flashcards
What’s the difference between Dart and Flutter
- Dart is a client optimized, object-oriented programming language. It is popular nowadays because of flutter. It is difficult to build complete apps only using Dart because you have to manage many things yourself.
- Flutter is a framework that uses dart programming language. With the help of flutter, you can build apps for android, iOS, web, desktop, etc. The framework contains ready-made tools to make apps faster.
Source: https://dart-tutorial.com/introduction-and-basics/
What is a statement?
A statement is a command that tells a computer to do something. In Dart, you can end most statements with a semicolon ;
.
Source: https://dart-tutorial.com/introduction-and-basics/
What is an expression?
An Expression is a value or something that can be calculated as a value. The expression can be numbers, text, or some other type. For E.g.
a. 52 b. 5+5 c. 'Hello World.' d. num
Source: https://dart-tutorial.com/introduction-and-basics/
What is a keyword?
Keywords are reserved words that give special meaning to the dart compiler. For E.g. int, if, var, String, const, etc.
Source: https://dart-tutorial.com/introduction-and-basics/
What is an identifier?
Identifiers are names created by the programmer to define variables, functions, classes, etc. Identifiers shouldn’t be keywords and must have a unique name. For E.g. int age =19;
, here age is an identifier. You will learn more about identifiers later in this course.
Source: https://dart-tutorial.com/introduction-and-basics/
What’s a High-level programming language?
High-Level Programming Language is easy to learn, user-friendly, and uses English-like-sentence. For E.g. dart,c,java,etc.
Source: https://dart-tutorial.com/introduction-and-basics/
What’s a Low-level Programming Language?
Low-level programming language is hard to learn, non-user friendly, and deals with computer hardware components, e.g., machine and assembly language.
Note: Low-level languages are faster than high-level but hard to understand and debug.
Source: https://dart-tutorial.com/introduction-and-basics/
What is Dart?
- Dart is a free and open-source programming language. You don’t need to pay any money to run dart programs.
- Dart is a platform-independent language and supports almost every operating system such as windows, mac, and Linux.
- Dart is an object-oriented programming language and supports all oops features such as encapsulation, inheritance, polymorphism, interface, etc.
- Dart comes with a dart2js compiler which translates dart code to javascript code that runs on all modern browsers.
- Dart is a programming language used by flutter, the world’s most popular framework for building apps.
Source: https://dart-tutorial.com/introduction-and-basics/
What is a variable?
Variables are containers used to store value in the program. There are different types of variables where you can keep different kinds of values. Here is an example of creating a variable and initializing it.
// here variable name contains value John. var name = "John";
Source: https://dart-tutorial.com/introduction-and-basics/
What are the different variable types in Dart?
- String: For storing text value. E.g. “John” [Must be in quotes]
- int: For storing integer value. E.g. 10, -10, 8555 [Decimal is not included]
- double: For storing floating point values. E.g. 10.0, -10.2, 85.698 [Decimal is included]
- num: For storing any type of number. E.g. 10, 20.2, -20 [both int and double]
- bool: For storing true or false. E.g. true, false [Only stores true or false values]
- var: For storing any value. E.g. ‘Bimal’, 12, ‘z’, true
Source: https://dart-tutorial.com/introduction-and-basics/
What is the syntax for declaring a variable in Dart?
type variableName = value;
Examples
void main() { //Declaring Variables String name = "John"; String address = "USA"; num age = 20; // used to store any types of numbers num height = 5.9; bool isMarried = false; // printing variables value print("Name is $name"); print("Address is $address"); print("Age is $age"); print("Height is $height"); print("Married Status is $isMarried"); }
Output
Name is John Address is USA Age is 20 Height is 5.9 Married Status is false
Note: Always use the descriptive variable name. Don’t use a variable name like a, b, c because this will make your code more complex.
Source: https://dart-tutorial.com/introduction-and-basics/
What are the rules for creating variables in Dart?
- Variable names are case sensitive, i.e., a and A are different.
- A variable name can consist of letters and alphabets.
- A variable name cannot start with a number.
- Keywords are not allowed to be used as a variable name.
- Blank spaces are not allowed in a variable name.
- Special characters are not allowed except for the underscore (_) and the dollar ($) sign.
Source: https://dart-tutorial.com/introduction-and-basics/
What is a constant in Dart?
Constant is the type of variable whose value never changes. In programming, changeable values are mutable and unchangeable values are immutable. Sometimes, you don’t need to change the value once declared. Like the value of PI=3.14, it never changes. To create a constant in Dart, you can use the const keyword.
void main(){ const pi = 3.14; pi = 4.23; // not possible print("Value of PI is $pi"); }
Output
Constant variables can't be assigned a value.
Source: https://dart-tutorial.com/introduction-and-basics/
What is the naming convention for variables in Dart?
It is a good habit to follow the naming convention. In Dart Variables, the variable name should start with lower-case, and every second word’s first letter will be upper-case like num1, fullName, isMarried, etc. Technically, this naming convention is called lowerCamelCase.
// Not standard way var fullname = "John Doe"; // Standard way var fullName = "John Doe"; const pi = 3.14;
Source: https://dart-tutorial.com/introduction-and-basics/
What is a data type in Dart?
Data types help you to categorize all the different types of data you use in your code. For e.g. numbers, texts, symbols, etc. The data type specifies what type of value will be stored by the variable. Each variable has its data type. Dart supports the following built-in data types :
- Numbers
- Strings
- Booleans
- Lists
- Maps
- Sets
- Runes
- Null
Source: https://dart-tutorial.com/introduction-and-basics/
What are the different built-in data types in Dart?
In Dart language, there is the type of values that can be represented and manipulated. The data type classification is as given below:
Data Type | Keyword | Description
~~~
| Numbers | int, double, num | It represents numeric values |
| Strings | String | It represents a sequence of characters |
| Booleans | bool | It represents Boolean values true and false |
| Lists | List | It is an ordered group of items |
| Maps | Map | It represents a set of values as key-value pairs |
| Sets | Set | It is an unordered list of unique values of same types |
| Runes | runes | It represents Unicode values of String |
| Null | null | It represents null value |
~~~
Source: https://dart-tutorial.com/introduction-and-basics/
What is a Number in Dart?
When you need to store numeric value on dart, you can use either int or double. Both int and double are subtypes of num. You can use num to store both int or double value.
void main() { // Declaring Variables int num1 = 100; // without decimal point. double num2 = 130.2; // with decimal point. num num3 = 50; num num4 = 50.4; // For Sum num sum = num1 + num2 + num3 + num4; // Printing Info print("Num 1 is $num1"); print("Num 2 is $num2"); print("Num 3 is $num3"); print("Num 4 is $num4"); print("Sum is $sum"); }
Output
Num 1 is 100 Num 2 is 130.2 Num 3 is 50 Num 4 is 50.4 Sum is 330.59999999999997
Source: https://dart-tutorial.com/introduction-and-basics/
How do you round a double value to 2 decimal places?
The .toStringAsFixed(2)
is used to round the double value upto 2 decimal places in dart. You can round to any decimal places by entering numbers like 2, 3, 4, etc.
void main() { // Declaring Variables double prize = 1130.2232323233233; // valid. print(prize.toStringAsFixed(2)); }
Output
1130.22
Source: https://dart-tutorial.com/introduction-and-basics/
How do you create a String in Dart?
String helps you to store text data. You can store values like I love dart, New York 2140 in String. You can use single or double quotes to store string in dart.
void main() { // Declaring Values String schoolName = "Diamond School"; String address = "New York 2140"; // Printing Values print("School name is $schoolName and address is $address"); }
Output
School name is Diamond School and address is New York 2140
Source: https://dart-tutorial.com/introduction-and-basics/
How do you create a multi-line String in Dart?
If you want to create a multi-line String in dart, then you can use triple quotes with either single or double quotation marks.
void main() { // Multi Line Using Single Quotes String multiLineText = ''' This is Multi Line Text with 3 single quote I am also writing here. '''; // Multi Line Using Double Quotes String otherMultiLineText = """ This is Multi Line Text with 3 double quote I am also writing here. """; // Printing Information print("Multiline text is $multiLineText"); print("Other multiline text is $otherMultiLineText"); }
Output
Multiline text is This is Multi Line Text with 3 single quote I am also writing here. Other multiline text is This is Multi Line Text with 3 double quote I am also writing here.
Source: https://dart-tutorial.com/introduction-and-basics/
What are some special characters in a String?
Special Character | Work
~~~
| \t | Tab |
~~~
Example usage
void main() { // Using \n and \t print("I am from \nUS."); print("I am from \tUS."); }
Output
I am from US. I am from US.
\n | New Line |
Source: https://dart-tutorial.com/introduction-and-basics/
How to you create a Raw String in Dart?
You can create a raw string in dart. Special characters won’t work here. You must write r after equal sign.
void main() { // Set prize value num prize = 10; String withoutRawString = "The value of prize is \t $prize"; // regular String String withRawString =r"The value of prize is \t $prize"; // raw String print("Without Raw: $withoutRawString"); // regular result print("With Raw: $withRawString"); // with raw result }
Output
Without Raw: The value of prize is 10 With Raw: The value of prize is \t $prize
Source: https://dart-tutorial.com/introduction-and-basics/
What is type conversion in Dart?
In dart, type conversion allows you to convert one data type to another type. For e.g. to convert String to int, int to String or String to bool, etc.
Source: https://dart-tutorial.com/introduction-and-basics/
How do you convert a String to an Int in Dart?
You can convert String to int using int.parse() method. The method takes String as an argument and converts it into an integer.
void main() { String strvalue = "1"; print("Type of strvalue is ${strvalue.runtimeType}"); int intvalue = int.parse(strvalue); print("Value of intvalue is $intvalue"); // this will print data type print("Type of intvalue is ${intvalue.runtimeType}"); }
Output
void main() { String strvalue = "1"; print("Type of strvalue is ${strvalue.runtimeType}"); int intvalue = int.parse(strvalue); print("Value of intvalue is $intvalue"); // this will print data type print("Type of intvalue is ${intvalue.runtimeType}"); }
Source: https://dart-tutorial.com/introduction-and-basics/
How do you convert a String to a Double in Dart?
You can convert String to double using double.parse() method. The method takes String as an argument and converts it into a double.
void main() { String strvalue = "1.1"; print("Type of strvalue is ${strvalue.runtimeType}"); double doublevalue = double.parse(strvalue); print("Value of doublevalue is $doublevalue"); // this will print data type print("Type of doublevalue is ${doublevalue.runtimeType}"); }
Output
Type of strvalue is String Value of doublevalue is 1.1 Type of doublevalue is double
Source: https://dart-tutorial.com/introduction-and-basics/
How do you convert an Int to a String in Dart?
You can convert int to String using the toString() method. Here is example:
void main() { int one = 1; print("Type of one is ${one.runtimeType}"); String oneInString = one.toString(); print("Value of oneInString is $oneInString"); // this will print data type print("Type of oneInString is ${oneInString.runtimeType}"); }
Output
Type of one is int Value of oneInString is 1 Type of oneInString is String
Source: https://dart-tutorial.com/introduction-and-basics/
How do you convert a Double to an Int in Dart?
You can convert double to int using the toInt() method.
void main() { double num1 = 10.01; int num2 = num1.toInt(); // converting double to int print("The value of num1 is $num1. Its type is ${num1.runtimeType}"); print("The value of num2 is $num2. Its type is ${num2.runtimeType}"); }
Output
The value of num1 is 10.01. Its type is double The value of num2 is 10. Its type is int
Source: https://dart-tutorial.com/introduction-and-basics/