构建 Flutter 原型很容易。构建可扩展、在负载下性能良好且可多年维护的生产就绪型 Flutter 应用程序需要对架构、状态管理、测试和部署工作流程有更深入的了解。本指南弥合了教程项目和实际应用程序之间的差距,涵盖了专业 Flutter 团队每天依赖的模式和实践。适用于 Flutter 的
Clean 架构
Clean 架构将您的应用程序分为具有清晰边界和依赖关系规则的不同层。这种分离使您的代码可测试、可维护并且独立于外部框架和工具。
层结构
生产型 Flutter 应用程序通常遵循三层架构:
- 表示层- 小部件、页面和状态管理。该层依赖于领域层,但从不直接依赖于数据源。
- 域层- 业务逻辑、实体和用例。该层对 Flutter 或任何外部包的依赖为零。它定义了数据层实现的存储库接口(抽象类)。
- 数据层- 存储库实现、API 客户端、本地数据库访问和数据模型 (DTO)。该层实现领域层中定义的接口。
lib/
core/
error/
exceptions.dart
failures.dart
network/
network_info.dart
usecases/
usecase.dart
features/
authentication/
data/
datasources/
auth_remote_datasource.dart
auth_local_datasource.dart
models/
user_model.dart
repositories/
auth_repository_impl.dart
domain/
entities/
user.dart
repositories/
auth_repository.dart
usecases/
login.dart
register.dart
logout.dart
presentation/
bloc/
auth_bloc.dart
auth_event.dart
auth_state.dart
pages/
login_page.dart
register_page.dart
widgets/
login_form.dart依赖关系规则很严格:内层永远不知道外层。领域层定义抽象存储库接口,数据层提供具体实现。这种控制反转允许您交换数据源而无需触及业务逻辑。
状态管理:BLoC 和 Riverpod
选择正确的状态管理解决方案是 Flutter 项目中最有影响力的架构决策之一。
BLoC 模式
BLoC(业务逻辑组件)使用流来管理状态。事件流入,状态流出。这种单向数据流使状态变化可预测且易于调试。
// Events
abstract class AuthEvent {}
class LoginRequested extends AuthEvent {
final String email;
final String password;
LoginRequested({required this.email, required this.password});
}
class LogoutRequested extends AuthEvent {}
// States
abstract class AuthState {}
class AuthInitial extends AuthState {}
class AuthLoading extends AuthState {}
class AuthAuthenticated extends AuthState {
final User user;
AuthAuthenticated(this.user);
}
class AuthError extends AuthState {
final String message;
AuthError(this.message);
}
// BLoC
class AuthBloc extends Bloc<AuthEvent, AuthState> {
final LoginUseCase loginUseCase;
final LogoutUseCase logoutUseCase;
AuthBloc({
required this.loginUseCase,
required this.logoutUseCase,
}) : super(AuthInitial()) {
on<LoginRequested>(_onLoginRequested);
on<LogoutRequested>(_onLogoutRequested);
}
Future<void> _onLoginRequested(
LoginRequested event,
Emitter<AuthState> emit,
) async {
emit(AuthLoading());
final result = await loginUseCase(
LoginParams(email: event.email, password: event.password),
);
result.fold(
(failure) => emit(AuthError(failure.message)),
(user) => emit(AuthAuthenticated(user)),
);
}
Future<void> _onLogoutRequested(
LogoutRequested event,
Emitter<AuthState> emit,
) async {
await logoutUseCase();
emit(AuthInitial());
}
}Riverpod
Riverpod 提供更灵活、编译安全的状态管理方法。与 Provider 不同,Riverpod 不依赖于 widget 树,从而更容易测试和组合。
// Define providers
final authRepositoryProvider = Provider<AuthRepository>((ref) {
return AuthRepositoryImpl(
remoteDatasource: ref.read(authRemoteDatasourceProvider),
localDatasource: ref.read(authLocalDatasourceProvider),
);
});
final authStateProvider = StateNotifierProvider<AuthNotifier, AuthState>((ref) {
return AuthNotifier(ref.read(authRepositoryProvider));
});
class AuthNotifier extends StateNotifier<AuthState> {
final AuthRepository _repository;
AuthNotifier(this._repository) : super(const AuthState.initial());
Future<void> login(String email, String password) async {
state = const AuthState.loading();
final result = await _repository.login(email, password);
state = result.fold(
(failure) => AuthState.error(failure.message),
(user) => AuthState.authenticated(user),
);
}
}
// Use in widgets
class LoginPage extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final authState = ref.watch(authStateProvider);
return authState.when(
initial: () => LoginForm(),
loading: () => const CircularProgressIndicator(),
authenticated: (user) => HomePage(user: user),
error: (message) => ErrorDisplay(message: message),
);
}
}依赖项注入
正确的依赖项注入对于可测试代码至关重要。get_it软件包提供了一个简单的服务定位器,可以很好地与干净的架构配合使用。
final sl = GetIt.instance;
void initDependencies() {
// External
sl.registerLazySingleton(() => Dio()..interceptors.add(AuthInterceptor()));
sl.registerLazySingleton(() => InternetConnectionChecker());
// Data sources
sl.registerLazySingleton<AuthRemoteDatasource>(
() => AuthRemoteDatasourceImpl(dio: sl()),
);
sl.registerLazySingleton<AuthLocalDatasource>(
() => AuthLocalDatasourceImpl(secureStorage: sl()),
);
// Repositories
sl.registerLazySingleton<AuthRepository>(
() => AuthRepositoryImpl(
remoteDatasource: sl(),
localDatasource: sl(),
networkInfo: sl(),
),
);
// Use cases
sl.registerLazySingleton(() => LoginUseCase(sl()));
sl.registerLazySingleton(() => RegisterUseCase(sl()));
// BLoCs
sl.registerFactory(() => AuthBloc(
loginUseCase: sl(),
logoutUseCase: sl(),
));
}API 与 Dio 集成
Dio 是最流行的 Dart HTTP 客户端,提供拦截器、全局配置和 FormData 支持。使用类型安全的请求和响应处理来构建 API 层。
class ApiClient {
final Dio _dio;
ApiClient(this._dio) {
_dio.options = BaseOptions(
baseUrl: Environment.apiBaseUrl,
connectTimeout: const Duration(seconds: 10),
receiveTimeout: const Duration(seconds: 15),
headers: {'Content-Type': 'application/json'},
);
_dio.interceptors.addAll([
AuthInterceptor(),
LogInterceptor(requestBody: true, responseBody: true),
RetryInterceptor(dio: _dio, retries: 3),
]);
}
Future<T> get<T>(
String path, {
Map<String, dynamic>? queryParameters,
required T Function(dynamic data) parser,
}) async {
try {
final response = await _dio.get(path, queryParameters: queryParameters);
return parser(response.data);
} on DioException catch (e) {
throw _handleError(e);
}
}
AppException _handleError(DioException error) {
switch (error.type) {
case DioExceptionType.connectionTimeout:
case DioExceptionType.receiveTimeout:
return NetworkException('Connection timed out');
case DioExceptionType.badResponse:
return ServerException(
error.response?.statusCode ?? 500,
error.response?.data?['message'] ?? 'Unknown error',
);
default:
return NetworkException('Network error occurred');
}
}
}具有 Hive 和 Sqflite 的本地存储
大多数生产应用程序需要本地数据持久性。根据您的数据复杂性选择正确的工具。
Hive,用于键值和对象存储
Hive 是一个用纯 Dart 编写的轻量级、速度极快的 NoSQL 数据库。它非常适合缓存、用户首选项和存储中小型数据集。
@HiveType(typeId: 0)
class CachedArticle extends HiveObject {
@HiveField(0)
final String id;
@HiveField(1)
final String title;
@HiveField(2)
final String content;
@HiveField(3)
final DateTime cachedAt;
CachedArticle({
required this.id,
required this.title,
required this.content,
required this.cachedAt,
});
}
class ArticleCacheService {
static const _boxName = 'articles_cache';
Future<void> cacheArticles(List<Article> articles) async {
final box = await Hive.openBox<CachedArticle>(_boxName);
final cached = articles.map((a) => CachedArticle(
id: a.id,
title: a.title,
content: a.content,
cachedAt: DateTime.now(),
));
await box.clear();
await box.addAll(cached);
}
Future<List<CachedArticle>> getCachedArticles() async {
final box = await Hive.openBox<CachedArticle>(_boxName);
return box.values.toList();
}
}用于关系数据的 Sqflite
当您的数据具有复杂关系并且需要 SQL 查询时,Sqflite 为 Flutter 提供完整的 SQLite 实现。将其用于受益于联接、索引和事务的结构化数据。
推送通知
使用 Firebase 云消息传递 (FCM) 以及适当的权限处理和后台消息处理来实现推送通知。
class NotificationService {
final FirebaseMessaging _messaging = FirebaseMessaging.instance;
Future<void> initialize() async {
// Request permission
final settings = await _messaging.requestPermission(
alert: true,
badge: true,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
// Get FCM token
final token = await _messaging.getToken();
await _sendTokenToServer(token);
// Listen for token refresh
_messaging.onTokenRefresh.listen(_sendTokenToServer);
// Handle foreground messages
FirebaseMessaging.onMessage.listen(_handleForegroundMessage);
// Handle background/terminated message taps
FirebaseMessaging.onMessageOpenedApp.listen(_handleMessageTap);
}
}
void _handleForegroundMessage(RemoteMessage message) {
// Show local notification using flutter_local_notifications
FlutterLocalNotificationsPlugin().show(
message.hashCode,
message.notification?.title,
message.notification?.body,
const NotificationDetails(
android: AndroidNotificationDetails(
'default_channel',
'Default',
importance: Importance.high,
),
),
);
}
}深层链接
深层链接允许用户从外部 URL 直接导航到应用程序内的特定内容。 Flutter同时支持基于URI的深度链接和动态链接。
// Configure in MaterialApp
MaterialApp(
onGenerateRoute: (settings) {
final uri = Uri.parse(settings.name ?? '');
if (uri.pathSegments.first == 'product') {
final productId = uri.pathSegments[1];
return MaterialPageRoute(
builder: (_) => ProductDetailPage(id: productId),
);
}
if (uri.pathSegments.first == 'order') {
final orderId = uri.pathSegments[1];
return MaterialPageRoute(
builder: (_) => OrderTrackingPage(id: orderId),
);
}
return MaterialPageRoute(builder: (_) => const HomePage());
},
)要获得更强大的深度链接,请使用go_router软件包,该软件包提供具有深度链接支持、重定向和嵌套导航的声明式路由。
带 Codemagic 和 Fastlane 的 CI/CD
自动化构建和部署管道对于生产应用程序至关重要。 Codemagic 提供 Flutter 原生 CI/CD 服务,而 Fastlane 提供更多可定制的自动化。
Codemagic 配置
# codemagic.yaml
workflows:
production-release:
name: Production Release
max_build_duration: 60
environment:
flutter: stable
vars:
APP_STORE_CONNECT_KEY_ID: Encrypted(...)
GOOGLE_PLAY_SERVICE_ACCOUNT: Encrypted(...)
scripts:
- name: Install dependencies
script: flutter pub get
- name: Run tests
script: flutter test --coverage
- name: Build Android
script: flutter build appbundle --release
- name: Build iOS
script: |
flutter build ipa --release \
--export-options-plist=/path/to/ExportOptions.plist
artifacts:
- build/**/outputs/**/*.aab
- build/ios/ipa/*.ipa
publishing:
google_play:
credentials: $GOOGLE_PLAY_SERVICE_ACCOUNT
track: internal
app_store_connect:
api_key: $APP_STORE_CONNECT_KEY_IDFastlane 集成
Fastlane 提供对构建和提交过程的精细控制。为不同的发布阶段定义通道:
# fastlane/Fastfile
platform :ios do
desc "Deploy to TestFlight"
lane :beta do
build_flutter_app(target: "lib/main.dart")
upload_to_testflight(
skip_waiting_for_build_processing: true
)
end
desc "Deploy to App Store"
lane :release do
build_flutter_app(target: "lib/main.dart")
upload_to_app_store(
submit_for_review: true,
automatic_release: false
)
end
end性能分析
生产应用程序需要一致的性能。 Flutter DevTools 提供全面的分析功能。
- 小部件重建跟踪- 使用性能覆盖和开发工具来识别过度重建的小部件。应用
const构造函数和选择性状态管理来最大限度地减少重建。 - 帧渲染- 监控时间线视图以确保帧在 16ms (60fps) 或 8ms (120fps) 内渲染。寻找昂贵的构建、布局和油漆阶段。
- 内存分析- 跟踪内存分配以检测泄漏。常见的罪魁祸首包括未取消的流订阅、未处理的控制器和闭包中保留的引用。
- 启动性能- 使用
WidgetsBinding.instance.addPostFrameCallback推迟繁重的初始化。对于不立即需要的功能,使用deferred as导入的延迟加载。
// Profile-mode build for accurate performance measurement
// flutter run --profile
// Add performance overlay in debug builds
MaterialApp(
showPerformanceOverlay: true,
// ...
)结论
构建可投入生产的 Flutter 应用程序需要的不仅仅是了解小部件目录。 它需要深思熟虑的架构、强大的状态管理、全面的测试和自动化部署管道。通过采用干净的架构、投资适当的依赖项注入、实施彻底的错误处理以及建立 CI/CD 工作流程,您创建的应用程序不仅可以正常运行,而且可以长期维护和扩展。
首先尽早建立架构,从第一天开始编写测试,并在首次发布之前自动化部署管道。随着时间的推移,这些前期投资会不断增加,使您的团队能够更快地发布功能,减少回归,并对每个版本更有信心。