照着 retrofit 撸了一个 Dart & Flutter http 库,欢迎 PR https://github.com/trevorwang/retrofit.dart - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
trevorwang
V2EX    Flutter

照着 retrofit 撸了一个 Dart & Flutter http 库,欢迎 PR https://github.com/trevorwang/retrofit.dart

  •  1
     
  •   trevorwang
    trevorwang 2020-02-10 11:00:28 +08:00 7128 次点击
    这是一个创建于 2073 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Retrofit For Dart

    Pub Dart CI CircleCI Build Status

    retrofit.dart is a type conversion dio client generator using source_gen and inspired by Chopper and Retrofit.

    Usage

    Generator

    Add the generator to your dev dependencies

    dependencies: retrofit: any logger: any #for logging purpose dev_dependencies: retrofit_generator: any build_runner: any 

    Define and Generate your API

    import 'package:json_annotation/json_annotation.dart'; import 'package:retrofit/retrofit.dart'; import 'package:dio/dio.dart'; part 'example.g.dart'; @RestApi(baseUrl: "https://5d42a6e2bc64f90014a56ca0.mockapi.io/api/v1/") abstract class RestClient { factory RestClient(Dio dio, {String baseUrl}) = _RestClient; @GET("/tasks") Future<List<Task>> getTasks(); } @JsonSerializable() class Task { String id; String name; String avatar; String createdAt; Task({this.id, this.name, this.avatar, this.createdAt}); factory Task.fromJson(Map<String, dynamic> json) => _$TaskFromJson(json); Map<String, dynamic> toJson() => _$TaskToJson(this); } 

    then run the generator

    # dart pub run build_runner build # flutter flutter pub run build_runner build 

    Use it

    import 'package:logger/logger.dart'; import 'package:retrofit_example/example.dart'; import 'package:dio/dio.dart'; final logger = Logger(); void main(List<String> args) { final dio = Dio(); // Provide a dio instance dio.options.headers["Demo-Header"] = "demo header"; // config your dio headers globally final client = RestClient(dio); client.getTasks().then((it) => logger.i(it)); 

    More

    Type Conversion

    Before you use the type conversion, please make sure that a factory Task.fromJson(Map<String, dynamic> json) must be provided for each model class. json_serializable is the recommanded to be used as the serialization tool.

    ... @GET("/tasks") Future<List<Task>> getTasks(); } @JsonSerializable() class Task { String name; Task({this.name}); factory Task.fromJson(Map<String, dynamic> json) => _$TaskFromJson(json); } 

    HTTP Methods

    The HTTP methods in the below sample are supported.

     @GET("/tasks/{id}") Future<Task> getTask(@Path("id") String id); @PATCH("/tasks/{id}") Future<Task> updateTaskPart( @Path() String id, @Body() Map<String, dynamic> map); @PUT("/tasks/{id}") Future<Task> updateTask(@Path() String id, @Body() Task task); @DELETE("/tasks/{id}") Future<void> deleteTask(@Path() String id); @POST("/tasks") Future<Task> createTask(@Body() Task task); @POST("http://httpbin.org/post") Future<void> createNewTaskFromFile(@Part() File file); @POST("http://httpbin.org/post") @FormUrlEncoded() Future<String> postUrlEncodedFormData(@Field() String hello); 

    Get orignal HTTP reponse

     @GET("/tasks/{id}") Future<HttpResponse<Task>> getTask(@Path("id") String id) @GET("/tasks") Future<HttpResponse<List<Task>>>> getTasks() 

    HTTP Header

    • Add a HTTP header from the parameter of the method

       @GET("/tasks") Future<Task> getTasks(@Header("Content-Type") String contentType ); 
    • Add staitc HTTP headers

       @GET("/tasks") @Headers(<String, dynamic>{ "Content-Type" : "application/json", "Custom-Header" : "Your header" }) Future<Task> getTasks(); 

    Error Handling

    catchError(Object) should be used for capturing the exception and failed response. You can get the detailed response info from DioError.response.

     client.getTask("2").then((it){ logger.i(it); }).catchError((Object obj) { // non-200 error goes here. switch (obj.runtimeType) { case DioError: // Here's the sample to get the failed response error code and message final res = (obj as DioError).response; logger.e("Got error : ${res.statusCode} -> ${res.statusMessage}"); break; default: } }); } 
    1 条回复    2020-03-18 09:24:50 +08:00
    phantomlimb
        1
    phantomlimb  
       2020-03-18 09:24:50 +08:00
    楼主我给你提了好几个 bug,哈哈哈,修的都很快!
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5838 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 02:59 PVG 10:59 LAX 19:59 JFK 22:59
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86