Language: English | 中文简体

Packages: xanno


Use annotations to simplify development @GApi @GAssets @GColor @GEntity @GFormatter @GHttp @GIconFont @GProject @GRoute @GSize

Getting Started

1:auto create project, using annotation @GProject(),

import 'package:xanno/xanno.dart';

void main() {}
  • dev_dependencies:
    • source_gen: ^1.0.0
    • build_runner: ^1.12.2
    • xanno: ^0.0.5+1
  • run: flutter pub get
  • run: flutter pub get && flutter packages pub run build_runner build && flutter pub get

command ==》./

2:Network data entity object, using annotation @GEntity(json: "", auto: true), Generate the JSON to DART conversion and generate the API/Entity_Factory.entity. dart for internal use in network requests

@GEntity Example use of annotations
@GEntity(json: '''
    "name": "爸爸",
    "age": 30,
    "wife": {
        "name": "妈妈",
        "age": 28,
        "beautiful": true
    "childList": [
            "name": "孩子1",
            "age": 1,
            "sex": "男"
            "name": "孩子2",
            "age": 2,
            "sex": "女"
''', auto: true)
class JsonEntity {}

3:For pages that need to jump, page widgets annotate @groute ('/other', 'other home '), Generate the route/main.route.dart method

@GRoute Example use of annotations
@GRoute(url: '/', title: 'main')
class MainPage extends StatefulWidget {
  final String title;

  MainPage({Key key, this.title}) : super(key: key);

  _MainPageState createState() => _MainPageState();

class _MainPageState extends State<MainPage> {

  Widget build(BuildContext context) {
      return Container();

4:API/API_INTERface. dart APIInterface defines methods for network requests, The API/Api_Factory.Interface. dart ApiFactory class is automatically generated

@apiInterface/@GET/@POST Example use of annotations
@Interface(host: '')
abstract class ApiInterface {
  @GET(url: "/tags")
  @Extra({'extraKey': 1, 'extraKey2': '2'})
  Future<List<dynamic>> getTags(@CancelRequest() CancelToken cancelReq);

  @GET(url: "/tags")
  Stream<List<String>> getTagsAsStream();

  @GET(url: "/tasks")
  @Extra({'isLoading': true})
  Future<List<dynamic>> getTasks();

  @GET(url: "/tasks/{id}")
  Future<dynamic> getTask(@Path("id") String id);

  @PATCH(url: "/tasks/{id}")
  Future<dynamic> updateTaskPart(
      @Path() String id, @Body() Map<String, dynamic> map);

  @PUT(url: "/tasks/{id}")
  Future<dynamic> updateTask(@Path() String id, @Body() dynamic task);

  @DELETE(url: "/tasks/{id}")
  Future<void> deleteTask(@Path() String id);

  @POST(url: "/tasks")
  Future<dynamic> createTask(@Body() dynamic task);

  @POST(url: "/tasks")
  Future<List<dynamic>> createTasks(@Body() List<dynamic> tasks);

  @POST(url: "/tasks")
  Future<List<String>> createTaskNames(@Body() List<String> tasks);

  @POST(host: "")
  Future<void> createNewTaskFromFile(@Part() File file);

  @Headers({"accept": "image/jpeg"})
  @GET(host: "")
  Future<List<int>> getFile();

  @POST(host: "")
  Future<String> postUrlEncodedFormData(
    @Field() String hello, {
    @Field() String? gg,

  @HEAD(url: '/')
  Future<String> headRequest();

  @HEAD(url: '/')
  Future headRquest2();

  @HEAD(url: '/')
  Future<HttpResponse> headRquest3();

  @GET(url: "/task/group")
  Future<List<dynamic>> grouppedTaskByDate();

  @GET(url: "/task")
  Future<HttpResponse<List<dynamic>>> getTasksWithReponse();

  @DELETE(url: "/tasks/{id}")
  Future<HttpResponse<void>> deleteTaskWithResponse(@Path() String id);

  @POST(url: "/post")
  Future<String> postFormData(@Part() dynamic task, {@Part() File? file});

  @POST(url: "/post")
  Future<String> postFormData2(
      @Part() List<Map<String, dynamic>> task,
      @Part() List<String> tags,
      @Part(contentType: 'application/json') File file);

  @POST(url: "/post")
  Future<String> postFormData3(
      {@Part(value: "customfiles", contentType: 'application/json')
          List<File>? files,
          File? file});

  @POST(url: "/post")
  Future<String> postFormData6(
      {@Part(value: "customfiles") List<List<int>>? files,
      @Part() List<int>? file});

  @POST(url: "/post")
  Future<String> postFormData4(@Part() List<dynamic> tasks, @Part() File file);

  @POST(url: "/post")
  Future<String> postFormData5(
    @Part() List<dynamic> tasks,
    @Part() Map<String, dynamic> map,
    @Part() int a, {
    @Part() bool? b,
    @Part() double? c,
    @Part() String? d,

  @GET(url: '/demo')
  Future<String> queries(@Queries() Map<String, dynamic> queries);

  @GET(url: '/enums')
  Future<String> queryByEnum(@Query('tasks') dynamic query);

  @GET(url: "/get")
  Future<String> namedExample(@Query("apikey") String apiKey,
      @Query("scope") String scope, @Query("type") String type,
      {@Query("from") int? from});

  @POST(url: "/postfile")
    "Content-Type": "application/octet-stream",
    "Ocp-Apim-Subscription-Key": "abc"
  Future<String> postFile({
    @Body() File? file,
    @SendProgress() Function(int, int)? sendProgress,
    @ReceiveProgress() Function(int, int)? receiveProgress,

  @GET(url: "")
  Future<String> testCustomOptions(@DioOptions() Options options);

5: Automatic registration of @GAssets ('assets') resources, no need to manually add resources to YAML (currently only supports assets:), automatic mapping to common/assets_constant.assets.dart
6: Automatic formatting and detection of code over 300 lines @GFormatter (maxLine: 300), lib file plus all Dart source code formatting and detection.
7: Auto register @GIconfont (url: '// ') to generate widget/icon_font.iconfont
9: Auto Generate @GColor() Auto Collect all places using Color Uniform Management common/color_constant.color.dart
10: AutoGenerate @GSize () AutoCollect all places that use Size uniformly manage common/size_constant.size.dart
Annotations use examples
@GAssets(path: 'assets')
@GIconfont(url: '//')
class App extends StatefulWidget {
  App({Key key}) : super(key: key);

  _AppState createState() => _AppState();

class _AppState extends State<App> {
  Widget build(BuildContext context) {
    return MaterialApp(
        routes: routes(context),

related reference libraries and projects

  • observable_builder (lightweight Flutter state management library with support for local refreshes and state caching.)