Browse Source

修改dio请求通用报错

weicu
tanlongsheng 2 years ago
parent
commit
1f980089c9
  1. 5
      lib/core/translations/language/en_us.dart
  2. 5
      lib/core/translations/language/zh_cn.dart
  3. 5
      lib/data/services/db/sqflite_api.dart
  4. 67
      lib/data/services/network/auth_interceptor.dart
  5. 24
      lib/modules/meeting/controllers/meeting_controller.dart
  6. 3
      lib/modules/meeting/model/meeting_model.dart
  7. 23
      lib/modules/meeting/views/meeting_view.dart
  8. 1
      pubspec.yaml

5
lib/core/translations/language/en_us.dart

@ -11,6 +11,11 @@ const Map<String, String> enUS = {
'connectionFailure': 'Connection Failed',
'unconnected': 'Disconnected',
'search': 'Search',
"requestCancellation": "Request cancellation",
"connectionTimeout": "Connection timeout",
"requestTimeout": "Request timeout",
"responseTimeout": "Response timeout",
"unknownError": "Unknown error",
// 启动页
"slogan": "Connect the World, Beyond Voice",

5
lib/core/translations/language/zh_cn.dart

@ -11,6 +11,11 @@ const Map<String, String> zhCN = {
'connectionFailure': '连接失败',
'unconnected': '未连接',
'search': '搜索',
"requestCancellation": "请求取消",
"connectionTimeout": "连接超时",
"requestTimeout": "请求超时",
"responseTimeout": "响应超时",
"unknownError": "未知错误",
// 启动页
"slogan": "连接世界,岂止声音",

5
lib/data/services/db/sqflite_api.dart

@ -1,3 +1,4 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'database_helper.dart';
@ -54,6 +55,8 @@ class SqfliteApi {
'error'.tr, // 错误
'unknownError'.tr,
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.red.withOpacity(0.1),
colorText: Colors.red,
);
return;
}
@ -87,6 +90,8 @@ class SqfliteApi {
'error'.tr, // 错误
'unknownError'.tr,
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.red.withOpacity(0.1),
colorText: Colors.red,
);
return;
}

67
lib/data/services/network/auth_interceptor.dart

@ -1,5 +1,5 @@
import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart' hide Response;
import 'package:get_storage/get_storage.dart';
@ -24,7 +24,14 @@ class AuthInterceptor extends Interceptor {
} else {
response.data = null;
handler.next(response);
return EasyLoading.showError(entity.message ?? 'unknownError'.tr);
Get.snackbar(
'error'.tr, // 错误
entity.message ?? 'unknownError'.tr,
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.red.withOpacity(0.1),
colorText: Colors.red,
);
return;
}
}
@ -33,32 +40,74 @@ class AuthInterceptor extends Interceptor {
switch (err.type) {
case DioExceptionType.cancel:
{
EasyLoading.showError('requestCancellation'.tr);
Get.snackbar(
'error'.tr, // 错误
'requestCancellation'.tr,
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.red.withOpacity(0.1),
colorText: Colors.red,
);
}
case DioExceptionType.connectionTimeout:
{
EasyLoading.showError('connectionTimeout'.tr);
Get.snackbar(
'error'.tr, // 错误
'connectionTimeout'.tr,
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.red.withOpacity(0.1),
colorText: Colors.red,
);
}
case DioExceptionType.sendTimeout:
{
EasyLoading.showError('requestTimeout'.tr);
Get.snackbar(
'error'.tr, // 错误
'requestTimeout'.tr,
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.red.withOpacity(0.1),
colorText: Colors.red,
);
}
case DioExceptionType.receiveTimeout:
{
EasyLoading.showError('responseTimeout'.tr);
Get.snackbar(
'error'.tr, // 错误
'responseTimeout'.tr,
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.red.withOpacity(0.1),
colorText: Colors.red,
);
}
case DioExceptionType.badResponse:
{
try {
String? errMsg = err.response?.statusMessage;
EasyLoading.showError(errMsg ?? 'unknownError'.tr);
Get.snackbar(
'error'.tr, // 错误
errMsg ?? 'unknownError'.tr,
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.red.withOpacity(0.1),
colorText: Colors.red,
);
} on Exception catch (_) {
EasyLoading.showError('unknownError'.tr);
Get.snackbar(
'error'.tr, // 错误
'unknownError'.tr,
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.red.withOpacity(0.1),
colorText: Colors.red,
);
}
}
default:
{
EasyLoading.showError(err.message ?? 'unknownError'.tr);
Get.snackbar(
'error'.tr, // 错误
err.message ?? 'unknownError'.tr,
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.red.withOpacity(0.1),
colorText: Colors.red,
);
}
}
handler.next(err);

24
lib/modules/meeting/controllers/meeting_controller.dart

@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../data/services/db/sqflite_api.dart';
class MeetingController extends GetxController {
final ScrollController listController = ScrollController();
@ -15,6 +17,8 @@ class MeetingController extends GetxController {
@override
void onInit() {
super.onInit();
listController.addListener(_onScroll);
_getDataList();
}
@override
@ -23,4 +27,24 @@ class MeetingController extends GetxController {
titleController.dispose();
super.onClose();
}
void _onScroll() {
if (isLoadingMore &&
listController.position.pixels >=
listController.position.maxScrollExtent - 100) {
page++;
_getDataList();
}
}
// 请求本地数据
void _getDataList() async {
List list = await SqfliteApi.getMeetingList(page);
dataList.addAll(list);
if (list.length < 10) {
isLoadingMore = false;
} else {
isLoadingMore = true;
}
}
}

3
lib/modules/meeting/model/meeting_model.dart

@ -4,7 +4,6 @@ class MeetingModel {
late int id;
late String title;
late String content;
late String language;
late int seconds;
late int creationtime;
@ -12,7 +11,6 @@ class MeetingModel {
..id = map["id"]
..title = map["title"]
..content = map["content"]
..language = map["language"]
..seconds = map["seconds"]
..creationtime = map["creationtime"];
@ -21,7 +19,6 @@ class MeetingModel {
data['id'] = id;
data['title'] = title;
data['content'] = content;
data['language'] = language;
data['seconds'] = seconds;
data['creationtime'] = creationtime;
return data;

23
lib/modules/meeting/views/meeting_view.dart

@ -56,9 +56,9 @@ class MeetingView extends GetView<MeetingController> {
itemCount: controller.dataList.length,
padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 5.w),
itemBuilder: (BuildContext context, int index) {
// MeetingModel data =
// MeetingModel.fromJson(controller.dataList[index]);
return _item(context, isDarkMode);
MeetingModel data =
MeetingModel.fromJson(controller.dataList[index]);
return _item(context, isDarkMode, data);
},
),
),
@ -94,7 +94,7 @@ class MeetingView extends GetView<MeetingController> {
);
}
Widget _item(BuildContext context, bool isDarkMode) {
Widget _item(BuildContext context, bool isDarkMode, MeetingModel data) {
return GestureDetector(
onTap: () {},
child: Container(
@ -120,7 +120,7 @@ class MeetingView extends GetView<MeetingController> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'data.title',
data.title,
style: TextStyle(
color: isDarkMode ? Colors.white : Colors.black87,
fontSize: 16.sp,
@ -128,7 +128,7 @@ class MeetingView extends GetView<MeetingController> {
overflow: TextOverflow.ellipsis,
),
Text(
'无内容',
data.content.isNotEmpty ? data.content : '无内容',
style: TextStyle(
color: isDarkMode ? Colors.white70 : Colors.black54,
fontSize: 12.sp,
@ -136,7 +136,10 @@ class MeetingView extends GetView<MeetingController> {
overflow: TextOverflow.ellipsis,
),
Text(
'12-13',
DateUtil.formatDateMs(
data.creationtime,
format: DateFormats.y_mo_d_h_m,
),
style: TextStyle(
color: isDarkMode ? Colors.white70 : Colors.black54,
fontSize: 12.sp,
@ -155,7 +158,11 @@ class MeetingView extends GetView<MeetingController> {
borderRadius: BorderRadius.circular(28.w),
),
child: Text(
'00:00:00',
DateUtil.formatDateMs(
data.seconds * 1000,
format: 'HH:mm:ss',
isUtc: true,
),
style: TextStyle(color: Colors.white, fontSize: 14.sp),
),
),

1
pubspec.yaml

@ -57,7 +57,6 @@ dependencies:
flutter_image_compress: ^2.4.0
# image_cropper: ^9.0.0
dio: ^5.8.0+1
flutter_easyloading: ^3.0.5
sqflite: ^2.4.2
path: ^1.9.1

Loading…
Cancel
Save