Question #1EasyDart BasicsImportant

Data types in flutter or dart

#flutter#dart

Answer

Overview

Dart is a statically typed language, meaning every variable has a type. Flutter/Dart supports a rich set of built-in data types.


Core Data Types

Numbers

dart
int age = 25;           // Whole numbers (64-bit signed)
double price = 9.99;    // Decimal numbers (64-bit floating point)
num mixed = 3;          // Either int or double
mixed = 3.14;           // ✅ num can hold both

// Int operations
print(10 ~/ 3);   // 3 (integer division)
print(10 % 3);    // 1 (modulo)
print(2.pow(8));  // Use dart:math: pow(2, 8) = 256

String

dart
String name = 'Alice';
String greeting = "Hello, $name!"; // Interpolation
String multiLine = '''
  This is a
  multi-line string
''';

// Common operations
print(name.length);          // 5
print(name.toUpperCase());   // ALICE
print(name.contains('Ali')); // true
print(name.substring(0, 3)); // Ali
print(name.split(''));        // [A, l, i, c, e]

Boolean

dart
bool isActive = true;
bool isLoggedIn = false;

// Dart requires explicit booleans (no truthy/falsy like JS)
if (isActive) print('Active');    // ✅
// if (1) print('truthy');        // ❌ Error in Dart

List (Array)

dart
List<int> numbers = [1, 2, 3, 4, 5];
var fruits = ['apple', 'banana', 'cherry']; // Type inferred

numbers.add(6);
numbers.removeAt(0);
print(numbers.length);
print(numbers[0]);
numbers.sort();

Map (Dictionary/HashMap)

dart
Map<String, int> scores = {'Alice': 95, 'Bob': 87};
var config = {'theme': 'dark', 'fontSize': 14};

scores['Charlie'] = 91;
print(scores['Alice']);           // 95
print(scores.containsKey('Bob')); // true

Set

dart
Set<String> tags = {'flutter', 'dart', 'mobile'};
tags.add('dart');         // Duplicate — ignored
print(tags.length);       // 3
print(tags.contains('flutter')); // true

Null

dart
int? nullableAge = null; // Nullable with ?
String? name = null;

// Null-safe access
print(name?.length);     // null (no crash)
print(name ?? 'Guest');  // Guest (default if null)

Dynamic & Object

dart
dynamic anything = 42;
anything = 'now a string'; // No type error

Object obj = 42;
obj = 'string'; // ✅ Object? can be null too

Type Summary

TypeExampleNotes
text
int
text
42
64-bit integer
text
double
text
3.14
64-bit float
text
num
text
3
or
text
3.14
int or double
text
String
text
'hello'
UTF-16
text
bool
text
true/false
Strictly boolean
text
List<T>
text
[1, 2, 3]
Ordered, indexed
text
Map<K,V>
text
{'a': 1}
Key-value
text
Set<T>
text
{1, 2, 3}
Unique elements
text
dynamic
AnyRuntime-typed
text
Null
text
null
Null type