TravelDao 接口多了params参数的缘故
来源:16-2 旅拍模块Dao层设计
rockyghu
2019-12-01
class TravelDao {
…
Map paramsMap = params[‘pagePara’];
paramsMap[‘pageIndex’] = pageIndex;
paramsMap[‘pageSize’] = pageSize;
…
}
写回答
1回答
-
CrazyCodeBoy
2019-12-02
1. Map params;是后期加入的一个参数,主要用于解决携程接口参数动态变化的问题,这个params参数是从老师的服务器上读取的入参配置:
在对应的课程代码:https://git.imooc.com/coding-321/flutter_trip/src/master/lib/pages/travel_page.dart#L81 中传递给TravelTabPage params: travelTabModel.params:
TravelTabPage( travelUrl: travelTabModel.url, params: travelTabModel.params, groupChannelCode: tab.groupChannelCode, )
在https://git.imooc.com/coding-321/flutter_trip/src/master/lib/pages/travel_tab_page.dart#L94 中将参数传递给调用接口的方法:
TravelDao.fetch(widget.travelUrl ?? _TRAVEL_URL, widget.params, widget.groupChannelCode, pageIndex, PAGE_SIZE) .then((TravelItemModel model) { _loading = false; setState(() { List<TravelItem> items = _filterItems(model.resultList); if (travelItems != null) { travelItems.addAll(items); } else { travelItems = items; } }); }).catchError((e) { _loading = false; print(e); });
3. 在TravelDao的fetch方法中使用params替换本地的Params:
///旅拍页接口 var Params = { "districtId": -1, "groupChannelCode": "RX-OMF", "type": null, "lat": -180, "lon": -180, "locatedDistrictId": 0, "pagePara": { "pageIndex": 1, "pageSize": 10, "sortType": 9, "sortDirection": 0 }, "imageCutType": 1, "head": {'cid': "09031014111431397988"}, "contentType": "json" }; class TravelDao { static Future<TravelItemModel> fetch( String url,Map params, String groupChannelCode, int pageIndex, int pageSize) async { Map paramsMap = params['pagePara']; paramsMap['pageIndex'] = pageIndex; paramsMap['pageSize'] = pageSize; params['groupChannelCode'] = groupChannelCode; final response = await http.post(url, body: jsonEncode(params)); if (response.statusCode == 200) { Utf8Decoder utf8decoder = Utf8Decoder(); // fix 中文乱码 var result = json.decode(utf8decoder.convert(response.bodyBytes)); return TravelItemModel.fromJson(result); } else { throw Exception('Failed to load travel'); } } }
10
相似问题