Dart Programming Lecture Notes PDF

Summary

This document provides a lecture on Dart programming, including various topics like data types, operators, and input/output examples for mobile application development. It covers concepts like dynamic variables, conditional statements, and more.

Full Transcript

Mobile Application Development Lec. 2 Dr. Samah Adel Outlines  Why Use Dart  Features of Dart Programming Language  Dart – Hello World Program  Dart – Comments  Dart – Variables  Dart – Operators  Dart – Standard Input Output  Data Types in Dart Why Use Dart Dart is a...

Mobile Application Development Lec. 2 Dr. Samah Adel Outlines  Why Use Dart  Features of Dart Programming Language  Dart – Hello World Program  Dart – Comments  Dart – Variables  Dart – Operators  Dart – Standard Input Output  Data Types in Dart Why Use Dart Dart is an open-source general-purpose programming language developed by Google. It supports application development on both the client and server side. However, it is widely used for the development of Android apps, iOS apps, IoT(Internet of Things), and web applications using the Flutter Framework. 1. Fast & Smooth: Dart compiles to native code for speedy performance, ideal for mobile apps. 2. Easy to Learn: Similar to familiar languages like Java or Javascript, making it approachable for new developers. 3. Flutter Power: Dart is the heart of Flutter, a popular framework for building beautiful and functional mobile apps. 4. One Code, Many Places: Develop for mobile, web, and even desktop with a single codebase (primarily with Flutter). Features of Dart Programming Language 1. Easy to Understand: as it provides the code Reuse it makes the Programs clean and easy to understand. 2. Object Oriented Programming: Nowadays OOPS is considered as one of the most important feature for many Programming Language including Java and C++. Now, Dart which is following the same path of OOPS. 3. Open Source: Dart is Open Source which makes it quite popular between the Indivisual and also for few big Organisations. 4. Browser Support: Dart Supported by all the browser because of its dart2js compiler that converts dart Program into JavaScript code which can be further processed by all the modern web browsers. 5. Type Safe: Dart is considered as type safe as it is the combination of static and runtime checks to ensure the variable value matches the variable type. 6. Flexible Compilation and Execution: Dart Compilation is flexible as it supports both JIT(Just-in-Time) compilation as well as AOT(Ahead of Time) compilation. And dart2js adds extra value to it. 7. Asynchronous Programming: Dart Supports Asynchronous Programming is where even when a Primary set of tasks are running , Program will respond to other set of tasks and at the end of the execution a final result will be returned. Hello World Program In dart main() function is a predefined method and acts as the entry point to the application. A dart script needs the main() method for execution of the code. The program code goes like this: main(): it is the symbol of main function that means the data entered in it is directly executed by compiler. print(“Hello World!”) : the role of print function is quite simple it just prints the data during the compilation of a program. Comments Types of Dart Comments: 1.Dart Single line Comment. 2.Dart Multiline Comment. 3.Dart Documentation Comment. Variables Types of Variables 1.Static Variable 2.Dynamic Variable 3.Final or const Dynamic Type Variable in Dart This is a special variable initialized with keyword dynamic. The variable declared with this data type can implicitly store any value while running the program. It is quite similar to var datatype in Dart, but the difference between them is the moment you assign the data to a variable with the var keyword it is replaced with the appropriate data type. Note: If we use var instead of dynamic in the above code then it will show error. Final And Const Keyword in Dart These keywords are used to define constant variable in Dart i.e. once a variable is defined using these keyword then its value can’t be changed in the entire code. These keyword can be used with or without data type name. 1. Final A final variable can only be set once and it is initialized when accessed. 2. Const A constant variable is a compile-time constant and its value must be known before the program runs. Null Safety in Dart In Dart, by default a variable can’t be assigned Null value till it is defined that the variable can store Null value in it. This to avoid cases where user assign null value in Dart. To declare a variable as nullable, you append a ‘?' to the type of the variable. The declared variable will by default store null as value and even after assigning it values of your choice you can declare it as null afterwards. Operators Different types of operators in Dart 1. Arithmetic Operators 2. Relational Operators 3. Type Test Operators 4. Bitwise Operators 5. Assignment Operators 6. Logical Operators 7. Conditional Operators Arithmetic Operators Relational Operators Type Test Operators Bitwise Operators Assignment Operators Logical Operators Conditional Operators Standard Input Output Input In Dart programming language, you can take standard input from the user through the console via the use of.readLineSync() function. To take input from the console you need to import a library, named dart:io from libraries of Dart. Standard Input Output Output In dart, there are two ways to display output in the console: 1.Using print statement. 2.Using stdout.write() statement. Example Data Types in Dart The data type classification is as given below: 1. Number The number in Dart Programming is the data type that is used to hold the numeric value. Dart numbers can be classified as: int: data type is used to represent whole numbers (64-bit Max). double: data type is used to represent 64-bit precise floating- point numbers. num: type is an inherited data type of the int and double types. 2. Strings in Dart A Dart string is a sequence of UTF-16 code units. With the same rule as that of Python, you can use either single or double quotes to create a string. The string starts with the datatype 3. Boolean It represents Boolean values true and false. The keyword bool is used to represent a Boolean literal in DART. 4. List List data type is similar to arrays in other programming languages. A list is used to represent a collection of objects. It is an ordered group of objects. Declaration of List There are multiple methods to declare List in Dart as mentioned below: 1. Variable Size List 2. Fixed Size List Fixed Size doesn’t mean that we can’t change the size of List, but we have predefined that the List has this much elements while declaring Example 5. Map The Map object is a key and value pair. Keys and values on a map may be of any type. It is a dynamic collection. While Declaring Map there can be only two cases one where declared Map is empty and another where declared Map contains elements in it. Both Cases are mentioned below: Example

Use Quizgecko on...
Browser
Browser