惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
J
Java Code Geeks
爱范儿
爱范儿
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
I
InfoQ
月光博客
月光博客
博客园 - 聂微东
博客园 - Franky
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
L
LangChain Blog
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Dart 3 New Features — Sealed Classes, Pattern Matching, a...
kanta13jp1 · 2026-04-29 · via DEV Community

kanta13jp1

Dart 3 New Features — Sealed Classes, Pattern Matching, and Records

Dart 3 changed how we write Dart. Master the three flagship features with real examples.

Records: Return Multiple Values Type-Safely

// Before Dart 3: needed a Map or a dedicated class
Map<String, dynamic> getUser() => {'name': 'Alice', 'age': 30};

// Dart 3: Records are type-safe
(String name, int age) getUser() => ('Alice', 30);

// Named fields
({String name, int age}) getUserNamed() => (name: 'Alice', age: 30);

// Usage
final user = getUser();
print(user.$1);  // 'Alice'
print(user.$2);  // 30

final named = getUserNamed();
print(named.name);  // 'Alice'

// Destructuring
final (name, age) = getUser();
print('$name is $age years old');

Enter fullscreen mode Exit fullscreen mode

Patterns: Structural Matching

// switch expression (returns a value)
String describe(Object value) => switch (value) {
  int n when n < 0  => 'negative',
  int n when n == 0 => 'zero',
  int _             => 'positive',
  String s          => 'string: $s',
  _                 => 'unknown',
};

// List pattern
final [first, second, ...rest] = [1, 2, 3, 4, 5];
print(first);  // 1
print(rest);   // [3, 4, 5]

// Map pattern
final {'name': String name, 'age': int age} = {'name': 'Bob', 'age': 25};
print('$name: $age');

// Object pattern
class Point { final double x, y; const Point(this.x, this.y); }

String describePoint(Point p) => switch (p) {
  Point(x: 0, y: 0)         => 'origin',
  Point(x: var x, y: 0)     => 'x-axis at $x',
  Point(x: 0, y: var y)     => 'y-axis at $y',
  Point(x: var x, y: var y) => '($x, $y)',
};

Enter fullscreen mode Exit fullscreen mode

Sealed Classes: Exhaustive Pattern Matching

// sealed class = subclassable only within the same library
sealed class Shape {}
class Circle    extends Shape { final double radius;        Circle(this.radius); }
class Rectangle extends Shape { final double w, h;          Rectangle(this.w, this.h); }
class Triangle  extends Shape { final double base, height;  Triangle(this.base, this.height); }

// The compiler verifies exhaustiveness — no else needed
double area(Shape shape) => switch (shape) {
  Circle(:final radius)              => 3.14 * radius * radius,
  Rectangle(:final w, :final h)     => w * h,
  Triangle(:final base, :final height) => base * height / 2,
};
// Forget Triangle → compile error (exhaustiveness check)

Enter fullscreen mode Exit fullscreen mode

In Practice: Type-Safe API Response Modeling

sealed class ApiResult<T> {}
class Success<T> extends ApiResult<T> { final T data;    Success(this.data); }
class Failure<T> extends ApiResult<T> { final String message; Failure(this.message); }
class Loading<T> extends ApiResult<T> {}

// Pattern-match in the UI
Widget buildWidget(ApiResult<User> result) => switch (result) {
  Loading()              => const CircularProgressIndicator(),
  Success(:final data)   => UserCard(user: data),
  Failure(:final message) => ErrorText(message),
};

Enter fullscreen mode Exit fullscreen mode

Summary

Records  → type-safe multiple return values: (T1, T2) or ({name: T1, age: T2})
Patterns → switch expression returns a value / List, Map, Object patterns
Sealed   → restrict inheritance + compile-time exhaustiveness / ideal for API state

Enter fullscreen mode Exit fullscreen mode

Combining all three Dart 3 features catches the majority of runtime errors at compile time.