Commit bcd47f5223023f4485fd1472420ac11dd321135a
1 parent
99efc573
fixed: 接口list范型支持
Showing
8 changed files
with
86 additions
and
79 deletions
lib/common/extension/exception_ext.dart
0 → 100644
lib/common/request/api_response/api_response_entity.g.dart
... | ... | @@ -19,7 +19,7 @@ ApiResponse<T> $ApiResponseFromJson<T>(Map<String, dynamic> json) { |
19 | 19 | print("ApiResponse<T> T-type:$type, data-type:${json['data'].runtimeType}"); |
20 | 20 | } |
21 | 21 | if (json['data'] != null) { |
22 | - data = jsonConvert.convert<T>(json['data']); | |
22 | + data = JsonConvert.fromJsonAsT(json['data']); | |
23 | 23 | } |
24 | 24 | if (data != null) { |
25 | 25 | apiResponseEntity.data = data; | ... | ... |
lib/common/request/dao/home_dao.dart
1 | 1 | import 'package:wow_english/common/request/apis.dart'; |
2 | 2 | import 'package:wow_english/common/request/request_client.dart'; |
3 | 3 | |
4 | +import '../../../models/course_module_entity.dart'; | |
5 | + | |
4 | 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 | 10 | return data; |
9 | 11 | } |
10 | 12 | ... | ... |
lib/common/request/request_client.dart
... | ... | @@ -148,8 +148,6 @@ class RequestClient { |
148 | 148 | raw.value = response.data; |
149 | 149 | return raw as T; |
150 | 150 | } else { |
151 | - print('response.data.runtimeType=${response.data.runtimeType}'); | |
152 | - | |
153 | 151 | // 应对response的header中content-type: [text/html;charset=utf-8]的情况 |
154 | 152 | // 实际上都是json格式返回 |
155 | 153 | Map<String, dynamic> json; |
... | ... | @@ -158,7 +156,6 @@ class RequestClient { |
158 | 156 | } else { |
159 | 157 | json = response.data; |
160 | 158 | } |
161 | - //ApiResponse<T> apiResponse = ApiResponse<T>.fromJson(json.decode(response.data)); | |
162 | 159 | ApiResponse<T> apiResponse = ApiResponse.fromJson(json); |
163 | 160 | onResponse?.call(apiResponse); |
164 | 161 | return _handleBusinessResponse<T>(apiResponse); | ... | ... |
lib/generated/json/base/json_convert_content.dart
... | ... | @@ -4,7 +4,7 @@ |
4 | 4 | |
5 | 5 | // This file is automatically generated. DO NOT EDIT, all your changes would be lost. |
6 | 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 | 8 | import 'package:wow_english/models/listen_entity.dart'; |
9 | 9 | import 'package:wow_english/models/user_entity.dart'; |
10 | 10 | |
... | ... | @@ -14,7 +14,7 @@ typedef EnumConvertFunction<T> = T Function(String value); |
14 | 14 | |
15 | 15 | class JsonConvert { |
16 | 16 | static final Map<String, JsonConvertFunction> convertFuncMap = { |
17 | - (LessonEntity).toString(): LessonEntity.fromJson, | |
17 | + (CourseModuleEntity).toString(): CourseModuleEntity.fromJson, | |
18 | 18 | (ListenEntity).toString(): ListenEntity.fromJson, |
19 | 19 | (UserEntity).toString(): UserEntity.fromJson, |
20 | 20 | }; |
... | ... | @@ -95,8 +95,8 @@ List<T>? convertListNotNull<T>(dynamic value, {EnumConvertFunction? enumConvert} |
95 | 95 | |
96 | 96 | //list is returned by type |
97 | 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 | 101 | if(<ListenEntity>[] is M){ |
102 | 102 | return data.map<ListenEntity>((Map<String, dynamic> e) => ListenEntity.fromJson(e)).toList() as M; |
... | ... | @@ -106,7 +106,7 @@ List<T>? convertListNotNull<T>(dynamic value, {EnumConvertFunction? enumConvert} |
106 | 106 | } |
107 | 107 | |
108 | 108 | debugPrint("${M.toString()} not found"); |
109 | - | |
109 | + | |
110 | 110 | return null; |
111 | 111 | } |
112 | 112 | |
... | ... | @@ -117,4 +117,4 @@ List<T>? convertListNotNull<T>(dynamic value, {EnumConvertFunction? enumConvert} |
117 | 117 | return jsonConvert.convert<M>(json); |
118 | 118 | } |
119 | 119 | } |
120 | -} | |
121 | 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 | 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 | 10 | final String? code = jsonConvert.convert<String>(json['code']); |
7 | 11 | if (code != null) { |
8 | - lessonEntity.code = code; | |
12 | + courseModuleEntity.code = code; | |
9 | 13 | } |
10 | 14 | final int? courseModuleThemeId = jsonConvert.convert<int>(json['courseModuleThemeId']); |
11 | 15 | if (courseModuleThemeId != null) { |
12 | - lessonEntity.courseModuleThemeId = courseModuleThemeId; | |
16 | + courseModuleEntity.courseModuleThemeId = courseModuleThemeId; | |
13 | 17 | } |
14 | 18 | final int? courseTotal = jsonConvert.convert<int>(json['courseTotal']); |
15 | 19 | if (courseTotal != null) { |
16 | - lessonEntity.courseTotal = courseTotal; | |
20 | + courseModuleEntity.courseTotal = courseTotal; | |
17 | 21 | } |
18 | 22 | final String? coverUrl = jsonConvert.convert<String>(json['coverUrl']); |
19 | 23 | if (coverUrl != null) { |
20 | - lessonEntity.coverUrl = coverUrl; | |
24 | + courseModuleEntity.coverUrl = coverUrl; | |
21 | 25 | } |
22 | 26 | final String? createTime = jsonConvert.convert<String>(json['createTime']); |
23 | 27 | if (createTime != null) { |
24 | - lessonEntity.createTime = createTime; | |
28 | + courseModuleEntity.createTime = createTime; | |
25 | 29 | } |
26 | 30 | final String? deleted = jsonConvert.convert<String>(json['deleted']); |
27 | 31 | if (deleted != null) { |
28 | - lessonEntity.deleted = deleted; | |
32 | + courseModuleEntity.deleted = deleted; | |
29 | 33 | } |
30 | 34 | final String? des = jsonConvert.convert<String>(json['des']); |
31 | 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 | 38 | final String? modifyTime = jsonConvert.convert<String>(json['modifyTime']); |
39 | 39 | if (modifyTime != null) { |
40 | - lessonEntity.modifyTime = modifyTime; | |
40 | + courseModuleEntity.modifyTime = modifyTime; | |
41 | 41 | } |
42 | 42 | final String? name = jsonConvert.convert<String>(json['name']); |
43 | 43 | if (name != null) { |
44 | - lessonEntity.name = name; | |
44 | + courseModuleEntity.name = name; | |
45 | 45 | } |
46 | 46 | final int? opening = jsonConvert.convert<int>(json['opening']); |
47 | 47 | if (opening != null) { |
48 | - lessonEntity.opening = opening; | |
48 | + courseModuleEntity.opening = opening; | |
49 | 49 | } |
50 | 50 | final String? picUrl = jsonConvert.convert<String>(json['picUrl']); |
51 | 51 | if (picUrl != null) { |
52 | - lessonEntity.picUrl = picUrl; | |
52 | + courseModuleEntity.picUrl = picUrl; | |
53 | 53 | } |
54 | 54 | final int? sortOrder = jsonConvert.convert<int>(json['sortOrder']); |
55 | 55 | if (sortOrder != null) { |
56 | - lessonEntity.sortOrder = sortOrder; | |
56 | + courseModuleEntity.sortOrder = sortOrder; | |
57 | 57 | } |
58 | 58 | final int? status = jsonConvert.convert<int>(json['status']); |
59 | 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 | 66 | final Map<String, dynamic> data = <String, dynamic>{}; |
67 | + data['id'] = entity.id; | |
67 | 68 | data['code'] = entity.code; |
68 | 69 | data['courseModuleThemeId'] = entity.courseModuleThemeId; |
69 | 70 | data['courseTotal'] = entity.courseTotal; |
... | ... | @@ -71,7 +72,6 @@ Map<String, dynamic> $LessonEntityToJson(LessonEntity entity) { |
71 | 72 | data['createTime'] = entity.createTime; |
72 | 73 | data['deleted'] = entity.deleted; |
73 | 74 | data['des'] = entity.des; |
74 | - data['id'] = entity.id; | |
75 | 75 | data['modifyTime'] = entity.modifyTime; |
76 | 76 | data['name'] = entity.name; |
77 | 77 | data['opening'] = entity.opening; |
... | ... | @@ -79,4 +79,4 @@ Map<String, dynamic> $LessonEntityToJson(LessonEntity entity) { |
79 | 79 | data['sortOrder'] = entity.sortOrder; |
80 | 80 | data['status'] = entity.status; |
81 | 81 | return data; |
82 | -} | |
83 | 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 | 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 | 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 | 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 | 1 | import 'package:flutter/cupertino.dart'; |
4 | -import 'package:flutter/foundation.dart'; | |
5 | 2 | import 'package:flutter_bloc/flutter_bloc.dart'; |
3 | +import 'package:flutter_easyloading/flutter_easyloading.dart'; | |
6 | 4 | import 'package:wow_english/common/request/dao/home_dao.dart'; |
7 | 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 | 7 | import 'package:wow_english/utils/loading.dart'; |
10 | 8 | |
11 | 9 | part 'lesson_event.dart'; |
12 | 10 | part 'lesson_state.dart'; |
13 | 11 | |
14 | 12 | class LessonBloc extends Bloc<LessonEvent, LessonState> { |
15 | - | |
16 | 13 | final int pageIndex; |
17 | 14 | |
18 | 15 | final PageController pageController; |
... | ... | @@ -21,10 +18,11 @@ class LessonBloc extends Bloc<LessonEvent, LessonState> { |
21 | 18 | |
22 | 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 | 26 | _currentPageIndex = pageIndex; |
29 | 27 | on<PageViewChangeIndexEvent>(_pageIndexChange); |
30 | 28 | } |
... | ... | @@ -32,23 +30,21 @@ class LessonBloc extends Bloc<LessonEvent, LessonState> { |
32 | 30 | Future<void> requestData() async { |
33 | 31 | try { |
34 | 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 | 41 | } catch (e) { |
45 | 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 | 48 | _currentPageIndex = event.index; |
53 | 49 | emitter(PageIndexChangeState()); |
54 | 50 | } | ... | ... |