前言
本文档详细介绍了一个完整的 Flutter + Spring Boot 全栈项目,包含跨平台移动应用和后端服务的完整技术实现。
一、项目架构
技术栈
1 2 3 4 5 6 7 8 9 10 11 12
| 前端:Flutter 3.x ── 状态管理:Riverpod ── 路由:GoRouter ├── 网络:Dio └── UI组件:Material Design 3
后端:Spring Boot 3.x ├── 框架:Spring WebFlux ├── 数据库:PostgreSQL ├── 缓存:Redis ├── 认证:JWT + Spring Security └── API文档:Swagger/OpenAPI
|
项目结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| project/ ├── mobile/ │ ├── lib/ │ │ ├── features/ │ │ ├── core/ │ │ ── shared/ │ └── test/ ├── backend/ │ ├── src/main/java/ │ │ ├── controller/ │ │ ├── service/ │ │ ├── repository/ │ │ └── model/ │ └── src/test/ └── docs/
|
二、核心功能
2.1 用户认证
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| final authProvider = Provider<AuthService>((ref) { return AuthService(dio: ref.watch(dioProvider)); });
class AuthService { Future<LoginResponse> login(String email, String password) async { final response = await dio.post('/auth/login', data: { 'email': email, 'password': password, }); return LoginResponse.fromJson(response.data); } }
|
1 2 3 4 5 6 7 8 9 10 11
| @PostMapping("/auth/login") public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) { Authentication auth = authenticationManager.authenticate( new UsernamePasswordAuthenticationToken( request.getEmail(), request.getPassword() ) ); String token = jwtTokenProvider.generateToken(auth); return ResponseEntity.ok(new LoginResponse(token)); }
|
2.2 数据同步
1 2 3 4 5 6 7 8 9 10
| class SyncService { Future<void> syncData() async { final localData = await localDb.getAll(); final serverData = await api.fetchUpdates(); final merged = mergeData(localData, serverData); await localDb.saveAll(merged); } }
|
三、部署方案
移动端部署
1 2 3 4 5
| flutter build apk --release
flutter build ios --release
|
后端部署
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| version: '3' services: api: build: ./backend ports: - "8080:8080" environment: - SPRING_PROFILES_ACTIVE=prod - DB_HOST=postgres - REDIS_HOST=redis postgres: image: postgres:15 environment: - POSTGRES_DB=myapp - POSTGRES_PASSWORD=secret redis: image: redis:7-alpine
|
四、性能优化
Flutter 优化
- 使用
const 构造函数
- 避免在 build 方法中创建对象
- 使用
ListView.builder 代替 ListView
- 图片使用
cached_network_image
Spring Boot 优化
- 使用 WebFlux 响应式编程
- Redis 缓存热点数据
- 数据库连接池优化
- 异步处理耗时操作
总结
这是一个完整的全栈项目模板,包含:
- ✅ 跨平台移动应用(iOS + Android)
- ✅ RESTful API 后端
- ✅ 离线优先架构
- ✅ 完整的认证授权
- ✅ 自动化部署配置
适合快速启动新的全栈项目。
本文档详细介绍了 Flutter + Spring Boot 全栈项目的技术实现和部署方案。