flutter retrofit plug网址 https://pub.dev/packages/retrofit
创建抽象类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13@RestApi(baseUrl: 'http://www.devio.org') abstract class Http{ factory Http(Dio dio,{String baseUrl}) = _HttpClient; @GET('/io/flutter_app/json/home_page.json') Future<HomeModel> getHomeDate(); @GET('/restapi/h5api/searchapp/search') Future getSearch(@Query('source') String source, @Query('action') String action, @Query('contentType') String contentType, @Query('keyword') String keyword,); }
具体类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23class _HttpClient implements Http{ _HttpClient(this._dio,{this.baseUrl}){ ArgumentError.checkNotNull(_dio,'dio'); this.baseUrl ??= 'http://www.devio.org';} final Dio _dio; String baseUrl; @override Future<HomeModel> getHomeDate() async { // TODO: implement getHomeDate final Response response =await _dio.request('/io/flutter_app/json/home_page.json', options: RequestOptions(method: 'GET', baseUrl: baseUrl)); return HomeModel.formJson(response.data); } @override Future getSearch(String source, String action, String contentType, String keyword) async{ // TODO: implement getSearch final Response response = await _dio.request('/restapi/h5api/searchapp/search', options: RequestOptions(method: 'GET',baseUrl: baseUrl) ); return response.data; }
方法调用
复制代码
1
2
3
4
5
6
7initDate() async { await Http(widget.dio).getHomeDate().then((value){ setState(() { homeModel = value; }); }); }
flutter retrofit @Query 传参
复制代码
1
2
3
4
5
6final queryParameters =<String,dynamic>{ 'source' : source, 'action' : action, 'contentType' : contentType, 'keyword' : keyword };
然后在
复制代码
1
2
3final Response response = await dio.request('/restapi/h5api/searchapp/search', options: RequestOptions(method: 'GET',baseUrl: baseUrl,queryParameters: queryParameters) );
最后
以上就是完美红牛最近收集整理的关于Flutter网络请求篇-dio-retrofit的全部内容,更多相关Flutter网络请求篇-dio-retrofit内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复