专业的编程技术博客社区

网站首页 > 博客文章 正文

关于 Flutter 中使用宏 macros

baijin 2024-12-23 10:24:02 博客文章 9 ℃ 0 评论

前言

需要使用 macros,则 dart 版本需要 3.5.0以上【Flutter SDK:3.23.0-0.1.pre以上】

使用前vscode配置

1、analysis_options.yaml文件中配置:
	analyzer:
  	enable-experiment:
    - macros

2、launch.json文件中配置:
		"args": ["--enable-experiment=macros"],

添加库:json

json: ^0.20.2

使用

import 'package:json/json.dart';

@JsonCodable() // 添加此注解
class UserModel {
  final String? name;
  final String? userName;
  final int? age;
  final List<WorksModel>? works;

  UserModel({
    this.name,
    this.userName,
    this.age,
    this.works = const [],
  });
}

@JsonCodable()
class WorksModel {
  final String? workName;
  final String? workDesc;
  final String? workProperty;

  WorksModel({
    this.workName,
    this.workDesc,
    this.workProperty,
  });
}
// json
final userJson = {
   'name': 'Marco',
    'userName': 'marco4527',
    'age': 20,
    'works': [
       {
         'workName': 'Flutter',
         'workDesc': 'Flutter is a UI toolkit for building beautiful, natively',
         'workProperty': 'URL_Flutter',
       },
       {
         'workName': 'Dart',
         'workDesc': 'Dart is a client-optimized language for fast apps on any platform',
         'workProperty': 'URL_Dart',
        },
      ],
    };

// 转模型
final user = UserModel.fromJson(userJson);
print(user.age);
print(user.name);
print(user.userName);
print(user.toJson());
print(user.works);

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表