← → or space · progress saves for Continue on the roadmap
Goal
Write reusable code that stays type-safe.
Step 1 - Typed collections
void main() {
List<int> numbers = [1, 2, 3];
Map<String, int> scores = {'Asha': 90, 'Rafi': 82};
print(numbers);
print(scores);
}List<int>accepts onlyint.Map<String, int>means key isString, value isint.
Step 2 - Type inference
void main() {
var names = <String>['A', 'B'];
var prices = <double>[9.99, 14.5];
print(names);
print(prices);
}- Dart can infer types from values and generic hints.
Step 3 - Simple generic function
T identity<T>(T value) {
return value;
}
void main() {
print(identity<int>(5));
print(identity<String>('hello'));
}- Same function works for many types.
dynamic vs Object (basic)
dynamic: no type checks at compile time, risky.Object: any object, but you must check/cast before specific use.- Prefer concrete types first. Use
Objectif needed. Avoiddynamicunless required.
Practice tasks
- Write
List<T> makePair<T>(T a, T b)and return[a, b]. - Create a typed map:
Map<String, double>. - Rewrite one
dynamicvariable into a safer specific type.