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)
);