Flutter 最佳实践
本节将介绍 Flutter 开发中的最佳实践和编码规范。
代码规范
命名规范
| 类型 | 规则 | 示例 |
|---|---|---|
| 类名 | 大驼峰 | MyHomePage |
| 函数/变量 | 小驼峰 | userName, getData |
| 常量 | 大驼峰 + k 前缀 | kMaxCount |
| 文件 | 小写 + 下划线 | my_home_page.dart |
实例:正确的命名
// 类名 - 大驼峰
class UserProfilePage extends StatelessWidget {}
// 函数 - 小驼峰
void fetchUserData() {}
// 变量 - 小驼峰
String userName = '张三';
// 常量 - k 前缀
const int kMaxRetryCount = 3;
const String kAppName = '我的应用';
// 枚举 - 大驼峰
enum AppState { loading, success, error }
// 文件名 - 小写下划线
// user_profile_page.dart
class UserProfilePage extends StatelessWidget {}
// 函数 - 小驼峰
void fetchUserData() {}
// 变量 - 小驼峰
String userName = '张三';
// 常量 - k 前缀
const int kMaxRetryCount = 3;
const String kAppName = '我的应用';
// 枚举 - 大驼峰
enum AppState { loading, success, error }
// 文件名 - 小写下划线
// user_profile_page.dart
性能优化
1. 避免不必要的 rebuild
实例:使用 const 构造函数
// 推荐:使用 const 减少 rebuild
const Text('Hello') // 不会重建
const SizedBox(width: 10)
// 避免:每次 build 都会创建新的对象
Text('Hello') // 会重建
// 在列表中使用 const
ListView(
children: const [
Text('A'),
Text('B'),
Text('C'),
],
)
const Text('Hello') // 不会重建
const SizedBox(width: 10)
// 避免:每次 build 都会创建新的对象
Text('Hello') // 会重建
// 在列表中使用 const
ListView(
children: const [
Text('A'),
Text('B'),
Text('C'),
],
)
2. 使用 keys 优化列表性能
实例:正确使用 keys
// 为列表项添加唯一 key
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
key: ValueKey(items[index].id), // 使用唯一标识作为 key
title: Text(items[index].name),
);
},
)
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
key: ValueKey(items[index].id), // 使用唯一标识作为 key
title: Text(items[index].name),
);
},
)
3. 延迟加载列表项
实例:使用 ListView.builder
// 推荐:使用 builder 懒加载
ListView.builder(
itemCount: 1000, // 大列表时必须使用
itemBuilder: (context, index) {
return ListTile(title: Text('Item $index'));
},
)
// 避免:使用 children 构建大列表
ListView(
children: List.generate(1000, (i) => Text('Item $i')),
)
ListView.builder(
itemCount: 1000, // 大列表时必须使用
itemBuilder: (context, index) {
return ListTile(title: Text('Item $index'));
},
)
// 避免:使用 children 构建大列表
ListView(
children: List.generate(1000, (i) => Text('Item $i')),
)
项目结构
实例:推荐的项目结构
// lib/
// ├── main.dart # 入口文件
// ├── app.dart # 应用配置
// ├── core/ # 核心功能
// │ ├── constants/ # 常量
// │ ├── theme/ # 主题
// │ ├── utils/ # 工具类
// │ └── extensions/ # 扩展
// ├── data/ # 数据层
// │ ├── models/ # 数据模型
// │ ├── repositories/ # 仓库
// │ └── services/ # 网络服务
// ├── domain/ # 领域层
// │ └── entities/ # 实体
// ├── presentation/ # 展示层
// │ ├── pages/ # 页面
// │ ├── widgets/ # 组件
// │ └── providers/ # 状态管理
// └── routes/ # 路由
// ├── main.dart # 入口文件
// ├── app.dart # 应用配置
// ├── core/ # 核心功能
// │ ├── constants/ # 常量
// │ ├── theme/ # 主题
// │ ├── utils/ # 工具类
// │ └── extensions/ # 扩展
// ├── data/ # 数据层
// │ ├── models/ # 数据模型
// │ ├── repositories/ # 仓库
// │ └── services/ # 网络服务
// ├── domain/ # 领域层
// │ └── entities/ # 实体
// ├── presentation/ # 展示层
// │ ├── pages/ # 页面
// │ ├── widgets/ # 组件
// │ └── providers/ # 状态管理
// └── routes/ # 路由
错误处理
实例:全局错误处理
void main() {
// 全局异常捕获
FlutterError.onError = (details) {
// 记录错误
print('Flutter Error: ${details.exception}');
// 上报错误到服务器
reportError(details.exception);
};
// Zone 异常捕获
runZonedGuarded(() {
runApp(const MyApp());
}, (error, stackTrace) {
print('Zone Error: $error');
reportError(error);
});
}
// 全局异常捕获
FlutterError.onError = (details) {
// 记录错误
print('Flutter Error: ${details.exception}');
// 上报错误到服务器
reportError(details.exception);
};
// Zone 异常捕获
runZonedGuarded(() {
runApp(const MyApp());
}, (error, stackTrace) {
print('Zone Error: $error');
reportError(error);
});
}
遵循最佳实践可以让代码更易维护、性能更优,减少生产环境中的 bug。
点我分享笔记