Loading...
Loading...
Dart programming for Flutter mobile and web development. Use for .dart files.
npx skill4agent add g1joshi/agent-skills dart.dartclass User {
final String id;
final String name;
final String email;
const User({required this.id, required this.name, required this.email});
factory User.fromJson(Map<String, dynamic> json) => User(
id: json['id'] as String,
name: json['name'] as String,
email: json['email'] as String,
);
}// Non-nullable by default
String name = 'John';
// Nullable with ?
String? maybeNull;
// Late initialization
late final String lazyInit;
// Null-aware operators
String greeting = person?.name ?? 'Guest';
person?.address?.city;// Records
(String, int) getPerson() => ('John', 25);
// Destructuring
final (name, age) = getPerson();
// Switch expressions with patterns
String describe(Object obj) => switch (obj) {
int i when i < 0 => 'negative',
int i => 'positive: $i',
String s => 'string: $s',
_ => 'unknown',
};
// Sealed classes
sealed class Result<T> {}
class Success<T> extends Result<T> { final T value; Success(this.value); }
class Failure<T> extends Result<T> { final String error; Failure(this.error); }Future<User> fetchUser(String id) async {
final response = await http.get(Uri.parse('$baseUrl/users/$id'));
if (response.statusCode != 200) {
throw Exception('Failed to load user');
}
return User.fromJson(jsonDecode(response.body));
}
// Parallel execution
Future<void> loadData() async {
final results = await Future.wait([
fetchUser('1'),
fetchOrders('1'),
]);
}
// Streams
Stream<int> countStream(int max) async* {
for (int i = 0; i < max; i++) {
await Future.delayed(Duration(seconds: 1));
yield i;
}
}extension StringExtension on String {
String capitalize() =>
isEmpty ? this : '${this[0].toUpperCase()}${substring(1)}';
bool get isValidEmail =>
RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$').hasMatch(this);
}constfinalrequireddynamic!late| Error | Cause | Solution |
|---|---|---|
| Using | Add null check first |
| Accessing late var before init | Initialize before access |
| Type mismatch with null | Check nullable types |