概述
flutter retrofit plug网址 https://pub.dev/packages/retrofit
创建抽象类
@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,);
}
具体类
class _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;
}
方法调用
initDate() async {
await Http(widget.dio).getHomeDate().then((value){
setState(() {
homeModel = value;
});
});
}
flutter retrofit @Query 传参
final queryParameters =<String,dynamic>{ 'source' : source, 'action' : action, 'contentType' : contentType, 'keyword' : keyword };
然后在
final Response response = await dio.request('/restapi/h5api/searchapp/search', options: RequestOptions(method: 'GET',baseUrl: baseUrl,queryParameters: queryParameters) );
最后
以上就是完美红牛为你收集整理的Flutter网络请求篇-dio-retrofit的全部内容,希望文章能够帮你解决Flutter网络请求篇-dio-retrofit所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复