coding-dart
Dart 3: null safety, async/await, streams, isolates, Flutter integration, pub package manager
What this skill does
# 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 `late` for lazy initialization and `required` for parameters, e.g., `late String name;` to declare a variable that must be initialized before use.
- **Async/Await**: Handle asynchronous code with `Future` and `async` functions, such as `Future<void> fetchData() async { var data = await http.get('/api/data'); }`.
- **Streams**: Manage asynchronous data streams using `Stream` from `dart:async`, e.g., `Stream<int> countStream() async* { for (int i = 0; i < 5; i++) yield i; }`.
- **Isolates**: Run concurrent tasks with `Isolate` for CPU-bound operations, e.g., `Isolate.spawn(workerFunction, args);` to offload work.
- **Flutter Integration**: Build UI with widgets and state management, e.g., using `StatelessWidget` for 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 analyze` with flags like `--fatal-infos` to enforce strict checks; run scripts with `dart run bin/main.dart`. For async, import `dart:async` and use `Future.delayed(Duration(seconds: 1), () => print('Delayed'));`.
- **Pub Commands**: Add dependencies with `pub add http --major-version 1` for specific versions; update with `pub upgrade`; build packages with `pub build --release`.
- **Key APIs**: Access streams via `StreamController` for custom streams, e.g., `var controller = StreamController<int>(); controller.add(1);`. For isolates, use `Isolate.run` for simple tasks: `Isolate.run(() => computeHeavyTask());`.
- **Config Formats**: Edit `pubspec.yaml` for dependencies, e.g.:
```
dependencies:
http: ^1.0.0
```
Use environment variables for secrets, e.g., set `$API_KEY` and access via `String.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
1. **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 with `var data = await getData(); if (data != null) print(data);`.
2. **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).
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.