Dart First Then Flutter. Learn Dart From Scratch.

JOSEF YADUVANSHI
8 min readMar 7, 2022
dart

Contents of this post.

  • What is a dart language?
  • Why learn Dart first before Flutter?
  • Hello World Program
  • Input & OutPut in Dart
  • Data Types and Variables in Dart
  • Operators
  • Arrays (List)
  • Conditional Statements
  • Functions
  • Classes
  • Start Flutter

What is a dart language?

Dart is an Object-Oriented, Class-based Programming language that uses C-style syntax. It was first released on Nov 14, 2013, by google and founded by Lars Bak and Kasper Lund. It is used for mobile, client-server, web, windows application development.

Why learn Dart first before Flutter?

A design that allows for creative flexibility has made Flutter popular among developers, making it easy to construct UIs for Mobile, Web and now with Flutter 2.10 update Windows applications. It uses the Dart platform as its primary base of operations and combines new development techniques like reactive programming and widget creation. Learning Dart before Flutter can help get a better grasp of the framework quickly. Moreover, if you are a beginner in programming, the Dart can easily catch up with and understand the basics.

Hello World Program

Let’s begin with the typical ‘Hello World’ program.

Hello World Program!

In the above code, in line 1, I have initiated the program with void used an entry point as main().

void

  • means null, which does not return any value.

main()

In line 1, main() is our entry point for the program. The main() method is Dart’s starting point. Note that the compiler searches for the main() function and runs its code when you start a Dart program. The software won’t execute if main() isn’t found. Curly brackets {} enclose the code in main(). {} the curly braces mean the start and end of the program. The left side is the start, and the right is the end. In lines 1 and line 5, you can see both the braces.

The basic syntax is given below:

Comments

You might notice I have used ‘//’ in line 2, to display a certain message. A comment is a programmer-readable explanation or annotation in a computer program’s source code. They are added to make the source code easier to read by humans, but compilers and interpreters ignore them. Comments are so helpful and useful. It helps you to keep track of your code as well as improve the code readability.

There are single-line comments and Multi-line comments, In the above syntax, I have used single-line comment. Below are the syntax for both types of comments.

// Single-line comments/* Multi-line comments */
Example of Single and Multi-line comments.

Now, you know about the entry point, comments, We have used one more statement ⤵️

Print

In line 2 of our code, we have used the Print statement, which, as per the name itself, prints/displays the string value, i.e. “Hello World😀!” in the console. The basic syntax of the print statement is:

Semi-colon

You will also notice after the parenthesis, we have used a semi-colon. Its role is to end the statement. It would be best never to miss a semi-colon in your program; otherwise, your program won’t work.

Output

Debug Console.

You can see from the above GIF; We got Hello World😀! as our output in the console.

Where to practice?

Well, you can practice in the VS Code by installing the Dart extension or in the Dart Pad officially provided by Google. I would recommend practising in VS Code as it will help you be more familiar with VS Code if you are a complete beginner.

Some Practice Questions for you😉:

Write a program to display the below word:

 I will never give up.

Input & OutPut in Dart

Input&Output in Dart.
I&O Example

Import "dart: io"; library.

Libraries in programming languages are collections of prewritten code that users can use to optimize tasks. In this case, this library helps us import the essentials for taking input from the user and displaying the output for the user.

Stdin

Stdin is a class and .readLineSync is a method. This class allows the user to read data from standard input in both synchronous and asynchronous ways. The method readLineSync() is one of the methods used to take input from the user. We will discuss about Classes and methods later in our lesson for now you can assume that it is necessary for taking input.

Input

As we discussed above stdin and readLineSync are used to take input from the user. In line 6 we used var ‘ans’ to take input from the user and later used it for displaying the output.

Output

There are two ways for displaying output in Dart.

  1. print();
  2. stdout.write();

In line 7, I have used print() to display the output. I have already explained the print() statement In the Hello World program.

In line 8, I have used stdout.write() to display the output. It is similar to the print() statement. It is also used to display the output to the user. It’s up to you which one you prefer.

You may notice, I have used the $ sign in the line 7 and 8 for importing the input data in the string value. Well, It can be used for importing any value in the string data.

Some Practice Questions for you😉:

Write a program to:1. display your name, by using stdin.readLineSync()
2. take any text or number as input from the user and display it.

Data Types and Variables in Dart.

Variables

Variables are used to store information for usage in a computer program. They also allow us to label data with descriptive names, helping both the reader and ourselves understand our applications. Consider variables as data containers. They only label and store data in memory. This information can then be used throughout the rest of the program.

Naming and Declaring a variable in Dart.

Well, there are specific rules and ways for declaring the variables, Below is the syntax:

dataType variableName = value

Example

Suppose you want to store a value of total student in your class, How would you do it in dart? Check ⤵️

Example

From the example above, In line 2, we have used a dataType int which means integer as we are storing a positive integer value. Next is our variableName and 50 is the value. Now, I hope you understood what is variable, and how to declare it.

You might notice, I have capitalized the first letter of each word except the first word, and used no separators. This naming convention is known as lowerCamelCase which we will use while working with Dart.

Data Types

In accordance with its name, a data type represents a particular sort of data that may be processed by a computer program in question. It might be strings, numeric, alphabetic, decimal, or any combination of these.

We will be discussing only the important and required data types needed for our use in flutter. They are:

  1. Int
  2. Double
  3. Num
  4. Bool
  5. String

Int

In accordance with its name, Int represents integer value which simply means all the positive and negative numbers excluding the floating/decimal numbers.

Example:

Int Example

Double

Doubles in dart represents decimal point numbers.

Example:

Double Example

Num

In accordance with its name, Num represents every number type, integer value which simply means all the positive and negative numbers including the floating/decimal numbers.

Example:

NUM Example

Bool

Bool are the booleans which denotes true or false, Or 0 or 1 where 0 is false and 1 is true.

Example:

Boolean Example

String

A Dart string uses UTF-16 code units. Dart string values can be represented by single, double, or triple quotes. Single or double quotations signify single line strings. The triple quotation denotes multi-line strings.

Example:

Strings Example

var

It is possible to declare a variable without explicitly declaring its data type thanks to type inference. Instead of a data type, Dart uses the var keyword. It means we don't need to specify the dataType, and The compiler will automatically depict the variable's Data Type.

Example

Variable Example

Constant and Final

A global variable or static variable can be declared (or a symbol defined in assembly) with a keyword qualifier such as const, or final, meaning that its value will be set at compile time and should not be changeable at runtime.

Operators

In computer programming, an operator is a symbol that often represents a process or action as a single entity. Mathematics and logic were drawn upon to create these symbols. A value or operand can be manipulated by an operator.

Types of Operators:

  1. Arithmetic Operators
  2. Relational Operators
  3. Type test Operators
  4. BitWise operators
  5. Logical Operators
  6. Conditional Operators

Arithmetic Operators

In accordance with its name, Arithmetic Operator represents every Mathematical calculations, like addition, substraction, division, modulus and multiplication.

Arithmetic Data

Example and Explanation:

Arithmetic Example

Some Practice Questions for you😉:

Write a program to:1. calculate the area of rectangle with the following data:
[ Length = 15, Breadth = 9, Area of rectangle = 2*(L+B) ]
2. calculate area of circle with the following data:
[ Radius = 8, const pi = 3.14, Area of circle = pi*r*r ]

Prefix and Postfix Operators

The supported prefix and postfix operators in Dart:

Prefix and Postfix Operators

PostFix Operators

The postfix decrement operator means that the expression is evaluated first using the variable’s initial value, and then the variable is decremented (decreased). The postfix Increment operator means that the expression is evaluated first using the variable’s initial value, and then the variable is increased.

Example:

Example PostFix

Explanation: In this situation, the print command displays the variable(Josef) value first, then increments it, so line 12 displays 7, and then line 13 displays the increased value i.e. 8. Similarly, For decrement, the print command displays the variable(Joseph) value first, then decrement it, so line 17 displays 8, and then line 13 displays the decreased value i.e. 7.

PreFix Operator

The prefix increment operator increments the variable first, then evaluates the expression using the new variable value. Prefix decrement operator means first decrement the variable, then evaluate the expression using the new value.

Example:

PreFix Operator Example

Explanation: Printing a variable’s value is accomplished by first decrementing it by one, and then printing its value. It directly increment/decrement the variable value and displays it. It doesn’t store its initial value like PostFix Operator.

Relational Operators

A relational operator is a programming language element that checks or establishes a relationship between two entities. It returns a boolean.

Relational Operators

Example and Explanation:

Example Relational Operators

Note: You may notice I have used ${operations} in the above example; well, you can use it for performing calculations in string data type.

Some Practice Questions for you😉:

Write a program to:1. Check if the square of 5 is greater than 26

Type test Operators

Type test operators are used to check an object’s type during runtime. The Dart type test operators are listed below. We will only discuss is and is not(is!) in this course.

TypeTest Operators

Example & Explanation:

Example TYPE TEST Operators.

BitWise Operators

A bitwise operator is an operator that is used to execute operations on bit patterns or binary numerals that require the manipulation of individual bits.

Bitwise Operators

Example and Explanation:

EXAMPLE AND EXPLANATION OF BITWISE OPERATORS

--

--

JOSEF YADUVANSHI

TECH ENTHUSIAST || APP DEVELOPMENT ❤️ || eatSleepCode — repeat👨🏻‍💻 || Okay BYEE!