Commit bcd47f5223023f4485fd1472420ac11dd321135a

Authored by Key
1 parent 99efc573

fixed: 接口list范型支持

lib/common/extension/exception_ext.dart 0 → 100644
  1 +import 'package:wow_english/common/request/exception.dart';
  2 +
  3 +extension ExceptionExtension on Exception {
  4 + String get getShowMessage {
  5 + if (this is ApiException) {
  6 + return (this as ApiException).message ?? '未知错误';
  7 + } else {
  8 + return '未知错误';
  9 + }
  10 + }
  11 +}
lib/common/request/api_response/api_response_entity.g.dart
@@ -19,7 +19,7 @@ ApiResponse<T> $ApiResponseFromJson<T>(Map<String, dynamic> json) { @@ -19,7 +19,7 @@ ApiResponse<T> $ApiResponseFromJson<T>(Map<String, dynamic> json) {
19 print("ApiResponse<T> T-type:$type, data-type:${json['data'].runtimeType}"); 19 print("ApiResponse<T> T-type:$type, data-type:${json['data'].runtimeType}");
20 } 20 }
21 if (json['data'] != null) { 21 if (json['data'] != null) {
22 - data = jsonConvert.convert<T>(json['data']); 22 + data = JsonConvert.fromJsonAsT(json['data']);
23 } 23 }
24 if (data != null) { 24 if (data != null) {
25 apiResponseEntity.data = data; 25 apiResponseEntity.data = data;
lib/common/request/dao/home_dao.dart
1 import 'package:wow_english/common/request/apis.dart'; 1 import 'package:wow_english/common/request/apis.dart';
2 import 'package:wow_english/common/request/request_client.dart'; 2 import 'package:wow_english/common/request/request_client.dart';
3 3
  4 +import '../../../models/course_module_entity.dart';
  5 +
4 class HomeDao { 6 class HomeDao {
5 ///获取课程模块信息 7 ///获取课程模块信息
6 - static Future<List<dynamic>?> courseModule() async {  
7 - var data = await requestClient.get<List<dynamic>?>(Apis.courseModule); 8 + static Future<List<CourseModuleEntity?>?> courseModule() async {
  9 + var data = await requestClient.get<List<CourseModuleEntity>>(Apis.courseModule);
8 return data; 10 return data;
9 } 11 }
10 12
lib/common/request/request_client.dart
@@ -148,8 +148,6 @@ class RequestClient { @@ -148,8 +148,6 @@ class RequestClient {
148 raw.value = response.data; 148 raw.value = response.data;
149 return raw as T; 149 return raw as T;
150 } else { 150 } else {
151 - print('response.data.runtimeType=${response.data.runtimeType}');  
152 -  
153 // 应对response的header中content-type: [text/html;charset=utf-8]的情况 151 // 应对response的header中content-type: [text/html;charset=utf-8]的情况
154 // 实际上都是json格式返回 152 // 实际上都是json格式返回
155 Map<String, dynamic> json; 153 Map<String, dynamic> json;
@@ -158,7 +156,6 @@ class RequestClient { @@ -158,7 +156,6 @@ class RequestClient {
158 } else { 156 } else {
159 json = response.data; 157 json = response.data;
160 } 158 }
161 - //ApiResponse<T> apiResponse = ApiResponse<T>.fromJson(json.decode(response.data));  
162 ApiResponse<T> apiResponse = ApiResponse.fromJson(json); 159 ApiResponse<T> apiResponse = ApiResponse.fromJson(json);
163 onResponse?.call(apiResponse); 160 onResponse?.call(apiResponse);
164 return _handleBusinessResponse<T>(apiResponse); 161 return _handleBusinessResponse<T>(apiResponse);
lib/generated/json/base/json_convert_content.dart
@@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
4 4
5 // This file is automatically generated. DO NOT EDIT, all your changes would be lost. 5 // This file is automatically generated. DO NOT EDIT, all your changes would be lost.
6 import 'package:flutter/material.dart' show debugPrint; 6 import 'package:flutter/material.dart' show debugPrint;
7 -import 'package:wow_english/models/lesson_entity.dart'; 7 +import 'package:wow_english/models/course_module_entity.dart';
8 import 'package:wow_english/models/listen_entity.dart'; 8 import 'package:wow_english/models/listen_entity.dart';
9 import 'package:wow_english/models/user_entity.dart'; 9 import 'package:wow_english/models/user_entity.dart';
10 10
@@ -14,7 +14,7 @@ typedef EnumConvertFunction&lt;T&gt; = T Function(String value); @@ -14,7 +14,7 @@ typedef EnumConvertFunction&lt;T&gt; = T Function(String value);
14 14
15 class JsonConvert { 15 class JsonConvert {
16 static final Map<String, JsonConvertFunction> convertFuncMap = { 16 static final Map<String, JsonConvertFunction> convertFuncMap = {
17 - (LessonEntity).toString(): LessonEntity.fromJson, 17 + (CourseModuleEntity).toString(): CourseModuleEntity.fromJson,
18 (ListenEntity).toString(): ListenEntity.fromJson, 18 (ListenEntity).toString(): ListenEntity.fromJson,
19 (UserEntity).toString(): UserEntity.fromJson, 19 (UserEntity).toString(): UserEntity.fromJson,
20 }; 20 };
@@ -95,8 +95,8 @@ List&lt;T&gt;? convertListNotNull&lt;T&gt;(dynamic value, {EnumConvertFunction? enumConvert} @@ -95,8 +95,8 @@ List&lt;T&gt;? convertListNotNull&lt;T&gt;(dynamic value, {EnumConvertFunction? enumConvert}
95 95
96 //list is returned by type 96 //list is returned by type
97 static M? _getListChildType<M>(List<Map<String, dynamic>> data) { 97 static M? _getListChildType<M>(List<Map<String, dynamic>> data) {
98 - if(<LessonEntity>[] is M){  
99 - return data.map<LessonEntity>((Map<String, dynamic> e) => LessonEntity.fromJson(e)).toList() as M; 98 + if(<CourseModuleEntity>[] is M){
  99 + return data.map<CourseModuleEntity>((Map<String, dynamic> e) => CourseModuleEntity.fromJson(e)).toList() as M;
100 } 100 }
101 if(<ListenEntity>[] is M){ 101 if(<ListenEntity>[] is M){
102 return data.map<ListenEntity>((Map<String, dynamic> e) => ListenEntity.fromJson(e)).toList() as M; 102 return data.map<ListenEntity>((Map<String, dynamic> e) => ListenEntity.fromJson(e)).toList() as M;
@@ -106,7 +106,7 @@ List&lt;T&gt;? convertListNotNull&lt;T&gt;(dynamic value, {EnumConvertFunction? enumConvert} @@ -106,7 +106,7 @@ List&lt;T&gt;? convertListNotNull&lt;T&gt;(dynamic value, {EnumConvertFunction? enumConvert}
106 } 106 }
107 107
108 debugPrint("${M.toString()} not found"); 108 debugPrint("${M.toString()} not found");
109 - 109 +
110 return null; 110 return null;
111 } 111 }
112 112
@@ -117,4 +117,4 @@ List&lt;T&gt;? convertListNotNull&lt;T&gt;(dynamic value, {EnumConvertFunction? enumConvert} @@ -117,4 +117,4 @@ List&lt;T&gt;? convertListNotNull&lt;T&gt;(dynamic value, {EnumConvertFunction? enumConvert}
117 return jsonConvert.convert<M>(json); 117 return jsonConvert.convert<M>(json);
118 } 118 }
119 } 119 }
120 -}  
121 \ No newline at end of file 120 \ No newline at end of file
  121 +}
lib/generated/json/lesson_entity.g.dart renamed to lib/generated/json/course_module_entity.g.dart
1 import 'package:wow_english/generated/json/base/json_convert_content.dart'; 1 import 'package:wow_english/generated/json/base/json_convert_content.dart';
2 -import 'package:wow_english/models/lesson_entity.dart'; 2 +import 'package:wow_english/models/course_module_entity.dart';
3 3
4 -LessonEntity $LessonEntityFromJson(Map<String, dynamic> json) {  
5 - final LessonEntity lessonEntity = LessonEntity(); 4 +CourseModuleEntity $CourseModuleEntityFromJson(Map<String, dynamic> json) {
  5 + final CourseModuleEntity courseModuleEntity = CourseModuleEntity();
  6 + final String? id = jsonConvert.convert<String>(json['id']);
  7 + if (id != null) {
  8 + courseModuleEntity.id = id;
  9 + }
6 final String? code = jsonConvert.convert<String>(json['code']); 10 final String? code = jsonConvert.convert<String>(json['code']);
7 if (code != null) { 11 if (code != null) {
8 - lessonEntity.code = code; 12 + courseModuleEntity.code = code;
9 } 13 }
10 final int? courseModuleThemeId = jsonConvert.convert<int>(json['courseModuleThemeId']); 14 final int? courseModuleThemeId = jsonConvert.convert<int>(json['courseModuleThemeId']);
11 if (courseModuleThemeId != null) { 15 if (courseModuleThemeId != null) {
12 - lessonEntity.courseModuleThemeId = courseModuleThemeId; 16 + courseModuleEntity.courseModuleThemeId = courseModuleThemeId;
13 } 17 }
14 final int? courseTotal = jsonConvert.convert<int>(json['courseTotal']); 18 final int? courseTotal = jsonConvert.convert<int>(json['courseTotal']);
15 if (courseTotal != null) { 19 if (courseTotal != null) {
16 - lessonEntity.courseTotal = courseTotal; 20 + courseModuleEntity.courseTotal = courseTotal;
17 } 21 }
18 final String? coverUrl = jsonConvert.convert<String>(json['coverUrl']); 22 final String? coverUrl = jsonConvert.convert<String>(json['coverUrl']);
19 if (coverUrl != null) { 23 if (coverUrl != null) {
20 - lessonEntity.coverUrl = coverUrl; 24 + courseModuleEntity.coverUrl = coverUrl;
21 } 25 }
22 final String? createTime = jsonConvert.convert<String>(json['createTime']); 26 final String? createTime = jsonConvert.convert<String>(json['createTime']);
23 if (createTime != null) { 27 if (createTime != null) {
24 - lessonEntity.createTime = createTime; 28 + courseModuleEntity.createTime = createTime;
25 } 29 }
26 final String? deleted = jsonConvert.convert<String>(json['deleted']); 30 final String? deleted = jsonConvert.convert<String>(json['deleted']);
27 if (deleted != null) { 31 if (deleted != null) {
28 - lessonEntity.deleted = deleted; 32 + courseModuleEntity.deleted = deleted;
29 } 33 }
30 final String? des = jsonConvert.convert<String>(json['des']); 34 final String? des = jsonConvert.convert<String>(json['des']);
31 if (des != null) { 35 if (des != null) {
32 - lessonEntity.des = des;  
33 - }  
34 - final String? id = jsonConvert.convert<String>(json['id']);  
35 - if (id != null) {  
36 - lessonEntity.id = id; 36 + courseModuleEntity.des = des;
37 } 37 }
38 final String? modifyTime = jsonConvert.convert<String>(json['modifyTime']); 38 final String? modifyTime = jsonConvert.convert<String>(json['modifyTime']);
39 if (modifyTime != null) { 39 if (modifyTime != null) {
40 - lessonEntity.modifyTime = modifyTime; 40 + courseModuleEntity.modifyTime = modifyTime;
41 } 41 }
42 final String? name = jsonConvert.convert<String>(json['name']); 42 final String? name = jsonConvert.convert<String>(json['name']);
43 if (name != null) { 43 if (name != null) {
44 - lessonEntity.name = name; 44 + courseModuleEntity.name = name;
45 } 45 }
46 final int? opening = jsonConvert.convert<int>(json['opening']); 46 final int? opening = jsonConvert.convert<int>(json['opening']);
47 if (opening != null) { 47 if (opening != null) {
48 - lessonEntity.opening = opening; 48 + courseModuleEntity.opening = opening;
49 } 49 }
50 final String? picUrl = jsonConvert.convert<String>(json['picUrl']); 50 final String? picUrl = jsonConvert.convert<String>(json['picUrl']);
51 if (picUrl != null) { 51 if (picUrl != null) {
52 - lessonEntity.picUrl = picUrl; 52 + courseModuleEntity.picUrl = picUrl;
53 } 53 }
54 final int? sortOrder = jsonConvert.convert<int>(json['sortOrder']); 54 final int? sortOrder = jsonConvert.convert<int>(json['sortOrder']);
55 if (sortOrder != null) { 55 if (sortOrder != null) {
56 - lessonEntity.sortOrder = sortOrder; 56 + courseModuleEntity.sortOrder = sortOrder;
57 } 57 }
58 final int? status = jsonConvert.convert<int>(json['status']); 58 final int? status = jsonConvert.convert<int>(json['status']);
59 if (status != null) { 59 if (status != null) {
60 - lessonEntity.status = status; 60 + courseModuleEntity.status = status;
61 } 61 }
62 - return lessonEntity; 62 + return courseModuleEntity;
63 } 63 }
64 64
65 -Map<String, dynamic> $LessonEntityToJson(LessonEntity entity) { 65 +Map<String, dynamic> $CourseModuleEntityToJson(CourseModuleEntity entity) {
66 final Map<String, dynamic> data = <String, dynamic>{}; 66 final Map<String, dynamic> data = <String, dynamic>{};
  67 + data['id'] = entity.id;
67 data['code'] = entity.code; 68 data['code'] = entity.code;
68 data['courseModuleThemeId'] = entity.courseModuleThemeId; 69 data['courseModuleThemeId'] = entity.courseModuleThemeId;
69 data['courseTotal'] = entity.courseTotal; 70 data['courseTotal'] = entity.courseTotal;
@@ -71,7 +72,6 @@ Map&lt;String, dynamic&gt; $LessonEntityToJson(LessonEntity entity) { @@ -71,7 +72,6 @@ Map&lt;String, dynamic&gt; $LessonEntityToJson(LessonEntity entity) {
71 data['createTime'] = entity.createTime; 72 data['createTime'] = entity.createTime;
72 data['deleted'] = entity.deleted; 73 data['deleted'] = entity.deleted;
73 data['des'] = entity.des; 74 data['des'] = entity.des;
74 - data['id'] = entity.id;  
75 data['modifyTime'] = entity.modifyTime; 75 data['modifyTime'] = entity.modifyTime;
76 data['name'] = entity.name; 76 data['name'] = entity.name;
77 data['opening'] = entity.opening; 77 data['opening'] = entity.opening;
@@ -79,4 +79,4 @@ Map&lt;String, dynamic&gt; $LessonEntityToJson(LessonEntity entity) { @@ -79,4 +79,4 @@ Map&lt;String, dynamic&gt; $LessonEntityToJson(LessonEntity entity) {
79 data['sortOrder'] = entity.sortOrder; 79 data['sortOrder'] = entity.sortOrder;
80 data['status'] = entity.status; 80 data['status'] = entity.status;
81 return data; 81 return data;
82 -}  
83 \ No newline at end of file 82 \ No newline at end of file
  83 +}
lib/models/lesson_entity.dart renamed to lib/models/course_module_entity.dart
1 -import 'package:wow_english/generated/json/base/json_field.dart';  
2 -import 'package:wow_english/generated/json/lesson_entity.g.dart';  
3 import 'dart:convert'; 1 import 'dart:convert';
4 2
  3 +import 'package:wow_english/generated/json/base/json_field.dart';
  4 +import 'package:wow_english/generated/json/course_module_entity.g.dart';
  5 +
5 @JsonSerializable() 6 @JsonSerializable()
6 -class LessonEntity {  
7 - String? code;  
8 - int? courseModuleThemeId;  
9 - int? courseTotal;  
10 - String? coverUrl;  
11 - String? createTime;  
12 - String? deleted;  
13 - String? des;  
14 - String? id;  
15 - String? modifyTime;  
16 - String? name;  
17 - int? opening;  
18 - String? picUrl;  
19 - int? sortOrder;  
20 - int? status; 7 +class CourseModuleEntity {
  8 + late String id;
  9 + String? code;
  10 + int? courseModuleThemeId;
  11 + int? courseTotal;
  12 + String? coverUrl;
  13 + String? createTime;
  14 + String? deleted;
  15 + String? des;
  16 + String? modifyTime;
  17 + String? name;
  18 + int? opening;
  19 + String? picUrl;
  20 + int? sortOrder;
  21 + int? status;
21 22
22 - LessonEntity(); 23 + CourseModuleEntity();
23 24
24 - factory LessonEntity.fromJson(Map<String, dynamic> json) => $LessonEntityFromJson(json); 25 + factory CourseModuleEntity.fromJson(Map<String, dynamic> json) => $CourseModuleEntityFromJson(json);
25 26
26 - Map<String, dynamic> toJson() => $LessonEntityToJson(this); 27 + Map<String, dynamic> toJson() => $CourseModuleEntityToJson(this);
27 28
28 - @override  
29 - String toString() {  
30 - return jsonEncode(this);  
31 - }  
32 -}  
33 \ No newline at end of file 29 \ No newline at end of file
  30 + @override
  31 + String toString() {
  32 + return jsonEncode(this);
  33 + }
  34 +}
lib/pages/lessons/bloc/lesson_bloc.dart
1 -import 'dart:convert';  
2 -  
3 import 'package:flutter/cupertino.dart'; 1 import 'package:flutter/cupertino.dart';
4 -import 'package:flutter/foundation.dart';  
5 import 'package:flutter_bloc/flutter_bloc.dart'; 2 import 'package:flutter_bloc/flutter_bloc.dart';
  3 +import 'package:flutter_easyloading/flutter_easyloading.dart';
6 import 'package:wow_english/common/request/dao/home_dao.dart'; 4 import 'package:wow_english/common/request/dao/home_dao.dart';
7 import 'package:wow_english/common/request/exception.dart'; 5 import 'package:wow_english/common/request/exception.dart';
8 -import 'package:wow_english/models/lesson_entity.dart'; 6 +import 'package:wow_english/models/course_module_entity.dart';
9 import 'package:wow_english/utils/loading.dart'; 7 import 'package:wow_english/utils/loading.dart';
10 8
11 part 'lesson_event.dart'; 9 part 'lesson_event.dart';
12 part 'lesson_state.dart'; 10 part 'lesson_state.dart';
13 11
14 class LessonBloc extends Bloc<LessonEvent, LessonState> { 12 class LessonBloc extends Bloc<LessonEvent, LessonState> {
15 -  
16 final int pageIndex; 13 final int pageIndex;
17 14
18 final PageController pageController; 15 final PageController pageController;
@@ -21,10 +18,11 @@ class LessonBloc extends Bloc&lt;LessonEvent, LessonState&gt; { @@ -21,10 +18,11 @@ class LessonBloc extends Bloc&lt;LessonEvent, LessonState&gt; {
21 18
22 int get currentPageIndex => _currentPageIndex; 19 int get currentPageIndex => _currentPageIndex;
23 20
24 - List<LessonEntity?> _listData = [];  
25 - List<LessonEntity?> get listData => _listData; 21 + List<CourseModuleEntity?> _listData = [];
26 22
27 - LessonBloc(this.pageIndex,this.pageController) : super(LessonInitial()) { 23 + List<CourseModuleEntity?> get listData => _listData;
  24 +
  25 + LessonBloc(this.pageIndex, this.pageController) : super(LessonInitial()) {
28 _currentPageIndex = pageIndex; 26 _currentPageIndex = pageIndex;
29 on<PageViewChangeIndexEvent>(_pageIndexChange); 27 on<PageViewChangeIndexEvent>(_pageIndexChange);
30 } 28 }
@@ -32,23 +30,21 @@ class LessonBloc extends Bloc&lt;LessonEvent, LessonState&gt; { @@ -32,23 +30,21 @@ class LessonBloc extends Bloc&lt;LessonEvent, LessonState&gt; {
32 Future<void> requestData() async { 30 Future<void> requestData() async {
33 try { 31 try {
34 await loading(() async { 32 await loading(() async {
35 - List list = await HomeDao.courseModule()??[];  
36 - // if (list.isNotEmpty) {  
37 - // for (dynamic jsonStr in list) {  
38 - // LessonEntity? data = LessonEntity.fromJson(jsonStr);  
39 - // _listData.add(data);  
40 - // }  
41 - // }  
42 - emit(LessonDataLoadState()); 33 + var list = await HomeDao.courseModule();
  34 + if (list?.isNotEmpty == true) {
  35 + _listData = list!;
  36 + emit(LessonDataLoadState());
  37 + } else {
  38 + EasyLoading.showToast("数据获取失败!");
  39 + }
43 }); 40 });
44 } catch (e) { 41 } catch (e) {
45 if (e is ApiException) { 42 if (e is ApiException) {
46 -  
47 } 43 }
48 } 44 }
49 } 45 }
50 46
51 - void _pageIndexChange(PageViewChangeIndexEvent event,Emitter<LessonState> emitter) async { 47 + void _pageIndexChange(PageViewChangeIndexEvent event, Emitter<LessonState> emitter) async {
52 _currentPageIndex = event.index; 48 _currentPageIndex = event.index;
53 emitter(PageIndexChangeState()); 49 emitter(PageIndexChangeState());
54 } 50 }