fix: 修复 Flutter Web 生产环境 API 地址未生效的问题
- NetworkConfig.baseUrl 改为使用 ApiEndpoints.baseUrl - 确保构建时 --dart-define=ENV=prod 能正确切换环境 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
75
flutter_monisuo/BUILD.md
Normal file
75
flutter_monisuo/BUILD.md
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
# Flutter 构建说明
|
||||||
|
|
||||||
|
## 环境配置
|
||||||
|
|
||||||
|
| 环境 | ENV | API 地址 |
|
||||||
|
|------|-----|----------|
|
||||||
|
| 开发 | `dev` | `http://localhost:5010` |
|
||||||
|
| 生产 | `prod` | `http://8.155.172.147:5010` |
|
||||||
|
|
||||||
|
## Web 构建
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 开发环境(默认)
|
||||||
|
flutter run -d chrome
|
||||||
|
|
||||||
|
# 生产构建
|
||||||
|
flutter build web --release --dart-define=ENV=prod
|
||||||
|
```
|
||||||
|
|
||||||
|
输出目录:`build/web/`
|
||||||
|
|
||||||
|
## Android 构建
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# APK (用于测试分发)
|
||||||
|
flutter build apk --release --dart-define=ENV=prod
|
||||||
|
|
||||||
|
# App Bundle (用于上架 Google Play)
|
||||||
|
flutter build appbundle --release --dart-define=ENV=prod
|
||||||
|
|
||||||
|
# 分架构构建 (减小体积)
|
||||||
|
flutter build apk --release --dart-define=ENV=prod --split-per-abi
|
||||||
|
```
|
||||||
|
|
||||||
|
输出目录:`build/app/outputs/`
|
||||||
|
|
||||||
|
## iOS 构建
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 需要 macOS
|
||||||
|
flutter build ios --release --dart-define=ENV=prod
|
||||||
|
```
|
||||||
|
|
||||||
|
## 常用参数
|
||||||
|
|
||||||
|
| 参数 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| `--release` | 生产模式,优化体积和性能 |
|
||||||
|
| `--debug` | 调试模式,含完整调试信息 |
|
||||||
|
| `--profile` | 性能分析模式 |
|
||||||
|
| `--dart-define=ENV=prod` | 设置生产环境 |
|
||||||
|
| `--split-per-abi` | 分架构构建 APK |
|
||||||
|
| `--wasm` | 启用 WebAssembly (实验性) |
|
||||||
|
|
||||||
|
## 代码中判断环境
|
||||||
|
|
||||||
|
```dart
|
||||||
|
import 'core/constants/api_endpoints.dart';
|
||||||
|
|
||||||
|
if (ApiEndpoints.isProduction) {
|
||||||
|
// 生产环境逻辑
|
||||||
|
} else {
|
||||||
|
// 开发环境逻辑
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 部署 Web
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 构建后上传 build/web 目录到服务器
|
||||||
|
scp -r build/web/* user@server:/var/www/html/
|
||||||
|
|
||||||
|
# 或使用 nginx 托管
|
||||||
|
# 将 build/web 目录配置为 nginx 静态资源目录
|
||||||
|
```
|
||||||
@@ -37,6 +37,6 @@ _flutter.buildConfig = {"engineRevision":"e4b8dca3f1b4ede4c30371002441c88c12187e
|
|||||||
|
|
||||||
_flutter.loader.load({
|
_flutter.loader.load({
|
||||||
serviceWorkerSettings: {
|
serviceWorkerSettings: {
|
||||||
serviceWorkerVersion: "3100103420" /* Flutter's service worker is deprecated and will be removed in a future Flutter release. */
|
serviceWorkerVersion: "2084931057" /* Flutter's service worker is deprecated and will be removed in a future Flutter release. */
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -93222,7 +93222,7 @@ return A.J($async$nV,r)}}
|
|||||||
A.CF.prototype={
|
A.CF.prototype={
|
||||||
J(a){var s=null,r=this.a8B()
|
J(a){var s=null,r=this.a8B()
|
||||||
return A.b7l(new A.zA(new A.Et(A.atZ(s,s,s,s,s,B.ap,s,s,s,s,B.a7v,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s),B.Jn,this.ga8q(),s),s),r)},
|
return A.b7l(new A.zA(new A.Et(A.atZ(s,s,s,s,s,B.ap,s,s,s,s,B.a7v,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s),B.Jn,this.ga8q(),s),s),r)},
|
||||||
a8B(){var s,r=null,q=new A.AI(),p=A.b3D("http://localhost:5010",B.qF,A.aD(["Content-Type","application/json"],t.N,t.z),B.qF),o=new A.QF(A.c([B.Ll],t.i6))
|
a8B(){var s,r=null,q=new A.AI(),p=A.b3D("http://8.155.172.147:5010",B.qF,A.aD(["Content-Type","application/json"],t.N,t.z),B.qF),o=new A.QF(A.c([B.Ll],t.i6))
|
||||||
o.a2(o,B.WA)
|
o.a2(o,B.WA)
|
||||||
s=new A.aer($,o,$,new A.ahG(51200),!1)
|
s=new A.aer($,o,$,new A.ahG(51200),!1)
|
||||||
s.Ya$=p
|
s.Ya$=p
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
|
import '../constants/api_endpoints.dart';
|
||||||
import '../storage/local_storage.dart';
|
import '../storage/local_storage.dart';
|
||||||
import 'api_exception.dart';
|
import 'api_exception.dart';
|
||||||
import 'api_response.dart';
|
import 'api_response.dart';
|
||||||
|
|
||||||
/// 网络配置常量
|
/// 网络配置常量
|
||||||
class NetworkConfig {
|
class NetworkConfig {
|
||||||
static const String baseUrl = 'http://localhost:5010';
|
static const String baseUrl = ApiEndpoints.baseUrl;
|
||||||
static const Duration connectTimeout = Duration(seconds: 30);
|
static const Duration connectTimeout = Duration(seconds: 30);
|
||||||
static const Duration receiveTimeout = Duration(seconds: 30);
|
static const Duration receiveTimeout = Duration(seconds: 30);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user