coding-dart
coding-dart
Purpose
This skill provides expertise in Dart 3 programming, focusing on features like null safety, async/await, streams, isolates, Flutter integration, and pub package management, to assist in writing efficient, safe code for apps and tools.
When to Use
Use this skill for Dart 3 projects involving mobile apps (e.g., Flutter), concurrent processing, asynchronous operations, or package dependencies. Apply it when null safety is critical to avoid runtime errors, or for handling real-time data with streams.
Key Capabilities
- Null Safety: Enforce non-nullable types using
latefor lazy initialization andrequiredfor parameters, e.g.,late String name;to declare a variable that must be initialized before use. - Async/Await: Handle asynchronous code with
Futureandasyncfunctions, such asFuture<void> fetchData() async { var data = await http.get('/api/data'); }. - Streams: Manage asynchronous data streams using
Streamfromdart:async, e.g.,Stream<int> countStream() async* { for (int i = 0; i < 5; i++) yield i; }. - Isolates: Run concurrent tasks with
Isolatefor CPU-bound operations, e.g.,Isolate.spawn(workerFunction, args);to offload work. - Flutter Integration: Build UI with widgets and state management, e.g., using
StatelessWidgetfor simple components. - Pub Package Manager: Manage dependencies via pub, including adding packages with
pub add package_name.
Usage Patterns
To accomplish tasks, structure code with null safety by always specifying types (e.g., String? optionalString; for nullable values). For async operations, wrap I/O in async functions and use await for readability. Use isolates for background tasks to avoid UI blocking. Integrate Flutter by starting with MaterialApp in main.dart. For package management, run pub get after editing pubspec.yaml. Always test with dart test before deployment.
Common Commands/API
- Dart CLI Commands: Use
dart analyzewith flags like--fatal-infosto enforce strict checks; run scripts withdart run bin/main.dart. For async, importdart:asyncand useFuture.delayed(Duration(seconds: 1), () => print('Delayed'));. - Pub Commands: Add dependencies with
pub add http --major-version 1for specific versions; update withpub upgrade; build packages withpub build --release. - Key APIs: Access streams via
StreamControllerfor custom streams, e.g.,var controller = StreamController<int>(); controller.add(1);. For isolates, useIsolate.runfor simple tasks:Isolate.run(() => computeHeavyTask());. - Config Formats: Edit
pubspec.yamlfor dependencies, e.g.:
Use environment variables for secrets, e.g., setdependencies: http: ^1.0.0$API_KEYand access viaString.fromEnvironment('API_KEY').
Integration Notes
Integrate Dart with Flutter by adding Flutter dependencies in pubspec.yaml and running flutter pub get. For external APIs, set environment variables like $FLUTTER_API_KEY and access in code via Platform.environment['FLUTTER_API_KEY']. Combine with other tools by using Dart's FFI for C libraries or integrating with VS Code via the Dart extension (install with code --install-extension Dart-Code.dart-code). Ensure Dart SDK is in PATH; verify with dart --version.
Error Handling
Handle errors in async code with try-catch in async functions, e.g.:
try {
var result = await fetchData();
} catch (e) {
print('Error: $e');
}
For streams, use onError callbacks: stream.listen((data) => print(data), onError: (error) => print(error)). Check null safety errors with dart analyze, and use rethrow to propagate exceptions. For isolates, handle communication errors with ReceivePort and SendPort.
Concrete Usage Examples
- Async HTTP Request: To fetch data safely with null safety, use:
import 'package:http/http.dart' as http; Future<String?> getData() async { try { var response = await http.get(Uri.parse('https://api.example.com/data')); return response.body; } catch (e) { return null; } }Then call it withvar data = await getData(); if (data != null) print(data);. - Flutter Widget with Stream: Create a simple counter stream in Flutter:
import 'dart:async'; class MyWidget extends StatelessWidget { Stream<int> counter() async* { for (int i = 0; i < 5; i++) yield i; } @override Widget build(BuildContext context) { return StreamBuilder<int>( stream: counter(), builder: (context, snapshot) => Text(snapshot.data?.toString() ?? '0'), ); } }.
Graph Relationships
- Related to cluster: coding (e.g., shares tags with coding-flutter for Flutter-specific tasks).
- Connected via tags: dart (links to general programming skills), flutter (integrates with UI-focused skills), coding (groups with other language skills like coding-python).