Numeric Types
- Integers: int score = 10;
- Doubles: double height = 5.9;
String Type
- String name = 'John Doe';
- String name = "Sandeep 1234";
Boolean Type
- bool isAdmin = true;
List Type
- List<String> colors = ['red', 'green', 'blue'];
Map Type
- Map<String, int> studentGrades = {'John': 90, 'Alice': 85};
Set Type
- Set<int> uniqueNumbers = {1, 2, 3, 3, 4}; (note: sets automatically remove duplicates)
Runes Type
- Runes unicodeChars = 'Hello, World!' .runes;
Symbol Type
- Symbol sym = #mySymbol;
void main()
{
int age=23;
double height=5.7;
print('Height:$height, Age: $age');
}
void main()
{
double a=-123123123.00;
double b=10.55465132465451324688465415;
double c=123123123.123123;
print('a=$a');
print('b=$b');
print('c=$c');
}
WADP to print the following string in a specific format Sample of given String
Output:
Twinkle, twinkle, little star,
How I wonder what you are!
Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star,
How I wonder what you are?
Code:
main() {
var value = '''
Twinkle, twinkle, little star,
\t How I wonder what you are!
\t\t Up above the world so high,
\t\t Like a diamond in the sky.
Twinkle, twinkle, little star,
\t How I wonder what you are?
''';
print(value);
}
WADP to print the following here document. Sample string : a string that you "don't" have to escape This is a ....... multi-line heredoc string --------> example
Code -
main() {
var output = '''
a string that you \"don\'t\" have to escape
this
is a ....... multi-line
heredoc string --------> example
''';
print(output);
}
Working with Strings
void main() {
String name = 'John Doe';
print('Name: $name');
// Concatenation
String greeting = 'Hello, ' + name;
print('Greeting: $greeting');
// Interpolation
String interpolatedGreeting = 'Hello, $name';
print('Interpolated Greeting: $interpolatedGreeting');
// Length
int length = name.length;
print('Length: $length');
// Upper case
String upperCase = name.toUpperCase();
print('Upper Case: $upperCase');
// Lower case
String lowerCase = name.toLowerCase();
print('Lower Case: $lowerCase');
}
void main()
{
bool isActive=true;
bool isCompleted=false;
print('is Active= $isActive, is Completed=$isCompleted');
}
void main()
{
bool a=true;
bool b=false;
print('True:$a');
print('False:$b');
}
void main( ){
var heart_symbol = '\u2665';
var laugh_symbol = '\u{1f600}';
print(heart_symbol);
print(laugh_symbol);
}
void main()
{
String s='a';
print(s.runes);
}
Here are 20 Dart programs showcasing various data types: Numeric, String, Boolean, Map, Set, List, and Runes. Each program demonstrates practical usage and manipulation.
void main() {
int a = 10;
double b = 3.14;
print('Sum: ${a + b}');
print('Multiplication: ${a * b}');
}
void main() {
String firstName = 'Sandeep';
String lastName = 'Kumar';
print('Full Name: $firstName $lastName');
}
void main() {
bool isSunny = true;
bool isWeekend = false;
print('Can I go out? ${isSunny && isWeekend}');
}
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
print('First number: ${numbers[0]}');
print('All numbers: $numbers');
}
void main() {
Map<String, int> age = {'Alice': 25, 'Bob': 30};
print('Alice\'s age: ${age['Alice']}');
}
void main() {
Set<int> uniqueNumbers = {1, 2, 2, 3};
print('Unique Numbers: $uniqueNumbers');
}
void main() {
Runes emoji = Runes('\u{1F600}');
print(String.fromCharCodes(emoji)); // 😀
}
void main() {
int x = 5;
int y = 2;
print('Division: ${x / y}');
print('Modulus: ${x % y}');
}
void main() {
String language = 'Dart';
print('I am learning $language programming!');
}
void main() {
bool isRainy = true;
print('Can I go out? ${!isRainy}');
}
void main() {
List<int> nums = [1, 2, 3];
nums.add(4);
print(nums);
}
void main() {
Map<String, String> capitals = {'India': 'Delhi', 'USA': 'Washington'};
print('Keys: ${capitals.keys}');
print('Values: ${capitals.values}');
}
void main() {
Set<int> a = {1, 2, 3};
Set<int> b = {3, 4, 5};
print('Union: ${a.union(b)}');
print('Intersection: ${a.intersection(b)}');
}
void main() {
double num = 5.7;
print('As int: ${num.toInt()}');
}
void main() {
String text = 'Hello World';
print('Length: ${text.length}');
print('Uppercase: ${text.toUpperCase()}');
}
void main() {
bool isAvailable = true;
if (isAvailable) {
print('The item is available!');
} else {
print('The item is not available!');
}
}
void main() {
List<dynamic> mixedList = [1, 'Hello', true];
print('Mixed List: $mixedList');
}
void main() {
Map<String, Map<String, int>> nestedMap = {
'Class1': {'Alice': 90, 'Bob': 85},
'Class2': {'John': 80, 'Doe': 95}
};
print('Class1 Marks: ${nestedMap['Class1']}');
}
void main() {
Set<int> nums = {1, 2, 3};
print('Contains 2: ${nums.contains(2)}');
}
void main() {
String heart = '\u2764';
String smiley = '\u{1F642}';
print('I $heart Dart $smiley');
}
void main()
{
Set<String> vowels={'A','E','I','O','U'};
print('vowels');
}
Ex=
void main()
{
// var st=<int>{1,2,2,3.3,"a"};
// var st=<var>{1,2,2,3.3,"b"};
var st=<dynamic>{1,2,2,3.3,"b"};
var st1=<double>{1,2.2,3,3};
var st2=<bool>{true,false};
print(st);
print(st1);
print(st2);
}
Ex=
void main()
{
var x = 23;
var y = 23;
var z = 45;
var list = <int>[];
list.add(x);
list.add(y);
list.add(z);
list.add(x);
print(list);
var set = <int>{};
set.add(x);
set.add(y);
set.add(z);
set.add(x);
print(set);
}
Prog.1:
void main()
{
List<String> fruits=['Apple','Banana','cherry'];
for(String fruit in fruits)
{
print(fruit);
}
}
Program 2: Working with Lists
void main() {
List<String> fruits = ['apple', 'banana', 'cherry'];
print('Fruits: $fruits');
// Add an element
fruits.add('date');
print('Fruits after add: $fruits');
// Remove an element
fruits.remove('banana');
print('Fruits after remove: $fruits');
// Get an element by index
String firstFruit = fruits[0];
print('First Fruit: $firstFruit');
// Check if an element exists
bool hasCherry = fruits.contains('cherry');
print('Has Cherry: $hasCherry');
}
void main()
{
var heart = '\u2665'; // Unicode for heart symbol
print('Heart symbol: $heart');
}
ex=
void main( ){
var heart_symbol = '\u2665';
var laugh_symbol = '\u{1f600}';
print(heart_symbol);
print(laugh_symbol);
}
ex=
void main()
{
String s='a';
print(s.runes);
}
void main() {
Map<int, String> student = {1: 'sandeep', 2: 'pradeep', 3: 'pooja'};
print(student);
}
Program 3: forEach with Maps
void main()
{
Map<int, String> student={1:'sandeep',2:'pradeep',3:'pooja'};
student.forEach((roll, name)
{
print('roll:$roll, name=$name');
});
}
Program 3: Working with Maps
void main() {
Map<String, int> personData = {'John': 30, 'Alice': 25};
print('Person Data: $personData');
// Add a new entry
personData['Bob'] = 40;
print('Person Data after add: $personData');
// Update an existing entry
personData['John'] = 31;
print('Person Data after update: $personData');
// Remove an entry
personData.remove('Alice');
print('Person Data after remove: $personData');
// Check if a key exists
bool hasJohn = personData.containsKey('John');
print('Has John: $hasJohn');
}
Program 4: Working with Sets
void main() {
Set<int> uniqueNumbers = {1, 2, 3, 3, 4};
print('Unique Numbers: $uniqueNumbers');
// Add an element
uniqueNumbers.add(5);
print('Unique Numbers after add: $uniqueNumbers');
// Remove an element
uniqueNumbers.remove(3);
print('Unique Numbers after remove: $uniqueNumbers');
// Check if an element exists
bool hasTwo = uniqueNumbers.contains(2);
print('Has Two: $hasTwo');
}
void main() {
List<int> numbers = [10, 20, 30];
print('First element: ${numbers[0]}');
print('All elements: $numbers');
}
void main() {
List<String> fruits = ['Apple', 'Banana'];
fruits.add('Orange');
fruits.remove('Banana');
print(fruits);
}
void main() {
List<int> numbers = [1, 2, 3];
for (var number in numbers) {
print(number);
}
}
void main() {
List<String> names = ['Alice', 'Bob'];
print(names.contains('Alice')); // true
}
void main() {
Map<String, int> scores = {'Alice': 85, 'Bob': 90};
print('Alice\'s score: ${scores['Alice']}');
}
void main() {
Map<String, String> capitals = {'India': 'Delhi'};
capitals['USA'] = 'Washington';
capitals.remove('India');
print(capitals);
}
void main() {
Map<String, int> data = {'A': 1, 'B': 2};
data.forEach((key, value) {
print('$key: $value');
});
}
void main() {
Map<String, Map<String, int>> students = {
'Class1': {'Alice': 90, 'Bob': 80},
'Class2': {'John': 85, 'Jane': 95},
};
print('Class1: ${students['Class1']}');
}
void main() {
Set<int> numbers = {1, 2, 3};
print('Set: $numbers');
print('Contains 2: ${numbers.contains(2)}');
}
void main() {
Set<String> animals = {'Dog', 'Cat'};
animals.add('Bird');
animals.remove('Cat');
print(animals);
}
void main() {
Set<int> setA = {1, 2, 3};
Set<int> setB = {3, 4, 5};
print('Union: ${setA.union(setB)}');
print('Intersection: ${setA.intersection(setB)}');
}
void main() {
List<int> numbers = [1, 2, 2, 3];
Set<int> uniqueNumbers = numbers.toSet();
print(uniqueNumbers); // {1, 2, 3}
}
void main() {
Runes emoji = Runes('\u{1F600}');
print(String.fromCharCodes(emoji)); // 😀
}
void main() {
String text = 'Hello 😊';
text.runes.forEach((int rune) {
print(String.fromCharCode(rune));
});
}
void main() {
Map<String, String> countries = {'India': 'Delhi'};
print(countries['USA'] ?? 'Not Found'); // Output: Not Found
}
void main() {
List<int> numbers = [1, 1, 2, 3, 3, 4];
Set<int> uniqueNumbers = numbers.toSet();
print(uniqueNumbers.toList()); // [1, 2, 3, 4]
}
void main() {
List<int> numbers = [5, 3, 8, 1];
numbers.sort();
print(numbers); // [1, 3, 5, 8]
}
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
List<int> evenNumbers = numbers.where((num) => num.isEven).toList();
print(evenNumbers); // [2, 4]
}
void main() {
Runes heart = Runes('\u2764');
Runes smile = Runes('\u{1F604}');
print(String.fromCharCodes(heart) + ' ' + String.fromCharCodes(smile)); // ❤️ 😄
}
WADP to parse a string to Float or Integer.
import 'dart:io';
main() {
var value = stdin.readLineSync()!;
int parse_int = int.parse(value);
double parse_double = double.parse(value);
print("Integer: $parse_int\n Double: $parse_double");
}
void main() {
dynamic variable = "Hello";
print(variable);
variable = 42; // Now it's an integer
print(variable);
variable = 4.2; // Now it's an FLOATING POINT
print(variable);
}
import 'dart:io';
void main()
{
int score=10; //number
double height=5.9;
print('score:=$score, height:=$height');
String name='sandeep'; //string
print('name:=$name');
bool isprogrammer =false; //boolean
print('isProgrammer: $isprogrammer');
List<String> color =['R','G','B','1', "1"]; //list type
print('Colour List:=$color');
Map<int, String> St_grd={90:'sandeep', 80:'pradeep', 70:'pooja'};//map type
print("Map grade:=$St_grd");
Set<int> uniqueNo={1,2,3,4,5,5};//set type
print('NumberSet is:=$uniqueNo');
Runes unicode='a, bc'.runes; //unicode type
print('Unicode:=$unicode');
Symbol sym=#mySchool; //symbol type
print('Symbol:=$sym');
}
void main() {
String? nullableName; // This can hold a String or null
if (nullableName == null) {
print('Name is NULL');
} else {
print('Name: $nullableName');
}
}