flutterannotations 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 44

FlutterAnnotations #

Description #

Fast Flutter Development. Easy maintenance.

Usage #

Create your REST API interface #

The Rest API works with a @Rest annotated interface. It's the entry point.

  1. Rest

       @Rest(rootUrl: "https://api.biminds.cn/")
       abstract class RestClient {
    
       }
    
  2. Annotate your own RestClient class with @Get

        @Rest(rootUrl: "https://api.biminds.cn/")
        abstract class RestClient {
    
         // OK
         @Get(path: "queryTeachers")
         List getData(@Param() String name,@Param() int id);
    
         // OK
         @Get(path: "queryTeachers/{name}")
         List getData(@Path() String name,@Param() int id);
    
         // OK
         @Get(path: "queryTeachers?name={name}")
         List getData(@Path() String name,@Param() int id);
    
         // OK
         @Get(path: "queryTeachers?name={name}&id={id}")
         Map getData(@Path() String name,@Param() int id);
    
         // OK
         @Get(path: "queryTeachers/{name}/{id}")
         dynamic getData(@Path() String name,@Param() int id); 
    
         // WRONG
         @Get(path: "queryTeachers")
         getData(@Body() String name);// Wrong, `@Body` can't be defined in `@Get`.
    
         // WRONG
         @Get(path: "queryTeachers/{name}/{id}")
         getData(@Path() String name); // Wrong, "id" must be defined with `@Path`.
    
         // WRONG
         @Get(path: "queryTeachers/{name}/{id}")
         getData(@Path() String name,@Param() int id); // Wrong, "id" must be defined with `@Path`.
    
         // WRONG
         @Get(path: "queryTeachers/{name}/{id}")
         Map getData(@Param() String name,@Param() int id); // Wrong, "name"  and "id" must be defined with `@Path`.
    
         // WRONG
         @Get(path: "queryTeachers")
         List<HomeBannerModel> getData<L>(@Param() String name,@Param() int id); // Wrong, method return must be HomeBannerModel
    
         // OK
         @Get(path: "queryTeachers")
         HomeBannerModel getData<L>(@Param() String name,@Param() int id); // OK, method return must be HomeBannerModel
    
         // WRONG
         @Get(path: "queryTeachers")
         Map<HomeBannerModel> getData<M>(@Param() String name,@Param() int id); // Wrong, method return must be HomeBannerModel
    
         // OK
         @Get(path: "queryTeachers")
         HomeBannerModel getData<M>(@Param() String name,@Param() int id); // OK, method return must be HomeBannerModel        
    
       }
    
  3. Annotate your own RestClient class with Post

       @Rest(rootUrl: "https://api.biminds.cn/")
       abstract class RestClient {
    
         // OK
         @Post(path: "updateTeacherInfo")
         Map postData(@Body() Map map);      
       }
    
  4. Annotate your own RestClient class with Delete

       @Rest(rootUrl: "https://api.biminds.cn/")
       abstract class RestClient {
    
         // OK
         @Delete(path: "deleteTeacherById")
         deleteDate(@Param() String name,@Param() int id);
    
         // OK
         @Delete(path: "deleteTeacherById/{name}")
         deleteDate(@Path() String name,@Param() int id);
    
         // OK
         @Delete(path: "deleteTeacherById?name={name}")
         deleteDate(@Path() String name,@Param() int id);
    
         // OK
         @Delete(path: "deleteTeacherById?name={name}&id={id}")
         deleteDate(@Path() String name,@Param() int id);
    
         // OK
         @Delete(path: "deleteTeacherById/{name}/{id}")
         deleteDate(@Path() String name,@Param() int id);
    
         // WRONG
         @Delete(path: "deleteTeacherById")
         deleteDate(@Body() String name);// Wrong, `@Body` can't be defined in `@Delete`.
    
         // WRONG
         @Delete(path: "deleteTeacherById/{name}/{id}")
         deleteDate(@Path() String name); // Wrong, "id" must be defined with `@Path`.
    
         // WRONG
         @Delete(path: "deleteTeacherById/{name}/{id}")
         deleteDate(@Path() String name,@Param() int id); // Wrong, "id" must be defined with `@Path`.
    
         // WRONG
         @Delete(path: "deleteTeacherById/{name}/{id}")
         deleteDate(@Param() String name,@Param() int id); // Wrong, "name"  and "id" must be defined with `@Path`.
    
       }
    
  5. Annotate your own RestClient class with Put

       @Rest(rootUrl: "https://api.biminds.cn/")
       abstract class RestClient {
    
         ///更新数据
         /// comment
         @Put(path: "updateTeacher")
         updateTeacher(@Body() Map map);
    
  }
6. run the command below in your workspace  
   build:

flutter packages pub run build_runner build --delete-conflicting-outputs

   suggest you running the clean command before build:  
    clean:

flutter packages pub run build_runner clean

Installation #

install from packages #

add packages to dev_dependencies in your pubspec.yaml
example:

dependencies:
  flutterannotations: ^1.0.1

install from source code #

clone the code, then put it into your workspace, announce it in your pubspec.yaml example:

dependencies:
  flutterannotations:
    path: flutterannotations

1.0.2 #

  • fix the parameter of 'failCallback'
  • fix the ' DioError [DioErrorType.RESPONSE]: data parsing exception...' bug

1.0.1 #

  • add interceptors in 'Rest' annotation
  • add the field 'name' which the annotations of method parameters
  • add the parameters of the 'Rest', resultData,resultMessage,resultCode
  • add header in 'Get'、'Post'、'Put'、'Delete' annotation
  • add @Header annotation as parameters
  • fix bugs

1.0.0 #

  • add return
  • fix bugs

0.0.1 #

  • TODO: Describe initial release.

example/lib/example.dart

//export 'package:flutterannotations_example/src/rest_client.dart';

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutterannotations: ^1.0.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutterannotations/flutterannotations.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
92
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
44
Learn more about scoring.

We analyzed this package on Dec 9, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21

Health suggestions

Fix lib/src/net/dio_util.dart. (-7.24 points)

Analysis of lib/src/net/dio_util.dart reported 15 hints, including:

line 16 col 23: Unnecessary new keyword.

line 38 col 23: Unnecessary new keyword.

line 132 col 12: Unnecessary new keyword.

line 162 col 32: Unnecessary new keyword.

line 165 col 35: Unnecessary new keyword.

Fix lib/src/net/log_of_format_interceptor.dart. (-0.50 points)

Analysis of lib/src/net/log_of_format_interceptor.dart reported 1 hint:

line 36 col 35: Unnecessary new keyword.

Maintenance issues and suggestions

Support latest dependencies. (-20 points)

The version constraint in pubspec.yaml does not support the latest published versions for 2 dependencies (analyzer, dio).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
analyzer ^0.36.0 0.36.4 0.39.2+1
dio ^2.1.0 2.2.2 3.0.7
Transitive dependencies
args 1.5.2
async 2.4.0
charcode 1.1.2
collection 1.14.12
convert 2.1.1
cookie_jar 1.0.1
crypto 2.1.4
csslib 0.16.1
front_end 0.1.19 0.1.29
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
kernel 0.3.19 0.3.29
meta 1.1.8
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
path 1.6.4
pedantic 1.9.0
pub_semver 1.4.2
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+13
yaml 2.2.0