跳到正文
教程首页
FlutterCH 09ch09_navigation

ch09-navigation

Flutter 路由与导航、资源与主题。完整工程共 103 个文件,其中 63 个文本源文件可在下面在线阅读。

63

在线文本文件

3,892

源码行数

dart / c / yaml

语言

280 KB

打包体积

在线阅读源码

左侧按目录浏览,点击文件切换。右上角可复制代码、复制路径或单独下载文件。在线视图只覆盖文本源码,完整工程请下载 zip。

textxmldartjsonkotlin

63 text files · 3,892 lines

dartlib/src/profile.dart
/// 资源里的领域模型:把"读文件"和"解析 JSON"分开。
///
/// 这不是过度设计——ch13 的网络层会照搬这个结构:
///   传输(rootBundle / http)→ 原始字符串 → 纯函数解析 → 模型
/// 解析是纯函数,可以脱离 I/O 单测;传输可以被 mock。
library;

import 'dart:convert';

import 'package:flutter/foundation.dart' show immutable;

@immutable
class Profile {
  const Profile({required this.name, required this.role, required this.tags});

  factory Profile.fromJson(Map<String, Object?> json) => Profile(
        name: json['name'] as String? ?? '未知',
        role: json['role'] as String? ?? '',
        tags: (json['tags'] as List<Object?>? ?? const <Object?>[])
            .whereType<String>()
            .toList(growable: false),
      );

  /// 纯函数:字符串 → 模型。不碰 I/O,测试里直接喂字符串。
  factory Profile.parse(String raw) =>
      Profile.fromJson(jsonDecode(raw) as Map<String, Object?>);

  final String name;
  final String role;
  final List<String> tags;

  @override
  String toString() => 'Profile($name, $role, tags: $tags)';
}

代码构成:text 60 KB · xml 41 KB · dart 27 KB · json 4 KB

本地运行

zip → 解压 → 执行
$cd examples/ch09-navigation && flutter run -d macos

这个 zip 里有什么

  • • 完整工程目录(含 Android / iOS / 桌面平台脚手架)
  • • lib / bin / test / native 全部源码与测试
  • • pubspec.yaml、锁文件与平台配置文件
  • • 已排除 .dart_tool / build / target / ephemeral 等构建产物

已验证

  • flutter analyze 0 告警、flutter test 14 个用例全绿、flutter build macos --debug 真实构建并启动成功
查看全部示例