我希望从提供的答案中获得更多详细信息。尽管它们是很好的建议,但它们对我来说太笼统了,无法理解。因此,在完成我自己的研究之后,我将分享我对上面提供的 JSON 示例的实现,希望它能节省其他人的时间。这是我遵循的步骤:
- 在我的 Flutter 项目中,首先我导入了以下库:
依赖项:
内置值:^1.0.1
内置集合:^1.0.0
开发依赖项:
构建运行者:^0.3.0
内置值生成器:^1.0.1
- 我创建了一个名为 tool 的文件夹。在其中,我放置了 2 个文件:build.dart 和 watch.dart。这些文件的实现如下所示
构建.dart
// Copyright (c) 2015, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import 'dart:async';
import 'package:build_runner/build_runner.dart';
import 'package:built_value_generator/built_value_generator.dart';
import 'package:source_gen/source_gen.dart';
/// Example of how to use source_gen with [BuiltValueGenerator].
///
/// Import the generators you want and pass them to [build] as shown,
/// specifying which files in which packages you want to run against.
Future main(List<String> args) async {
await build(
new PhaseGroup.singleAction(
new GeneratorBuilder([new BuiltValueGenerator()]),
new InputSet('built_value_example', const [
'lib/model/*.dart',
'lib/*.dart',
])),
deleteFilesByDefault: true);
}
手表.dart
// Copyright (c) 2016, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import 'dart:async';
import 'package:build_runner/build_runner.dart';
import 'package:built_value_generator/built_value_generator.dart';
import 'package:source_gen/source_gen.dart';
/// Example of how to use source_gen with [BuiltValueGenerator].
///
/// This script runs a watcher that continuously rebuilds generated source.
///
/// Import the generators you want and pass them to [watch] as shown,
/// specifying which files in which packages you want to run against.
Future main(List<String> args) async {
watch(
new PhaseGroup.singleAction(
new GeneratorBuilder([new BuiltValueGenerator()]),
new InputSet('built_value_example', const [
'lib/model/*.dart',
'lib/*.dart'])),
deleteFilesByDefault: true);
}
- 我创建了一个 serializers.dart 文件,它将我的 json 字符串序列化为我的自定义 dart 对象,以及我的模型对象 person.dart
序列化器.dart
library serializers;
import 'package:built_collection/built_collection.dart';
import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'model/person.dart';
part 'serializers.g.dart';
Serializers serializers = (
_$serializers.toBuilder()..addPlugin(new StandardJsonPlugin())
).build();
person.dart
library person;
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
part 'person.g.dart';
abstract class Person implements Built<Person, PersonBuilder> {
String get name;
int get age;
BuiltList<Car> get cars;
Person._();
factory Person([updates(PersonBuilder b)]) = _$Person;
static Serializer<Person> get serializer => _$personSerializer;
}
abstract class Car implements Built<Car, CarBuilder> {
String get name;
BuiltList<String> get models;
Car._();
factory Car([updates(CarBuilder b)]) = _$Car;
static Serializer<Car> get serializer => _$carSerializer;
}
-
创建以上4个文件后,会显示一些编译错误。暂时先别理会他们。这是因为 build.dart 文件尚未运行。所以在这一步中,运行build.dart。如果您使用的是 Webstorm,只需右键单击 build.dart 并点击“运行 build.dart”即可。这将创建 2 个文件:“person.g.dart”和“serializers.g.dart”。如果你仔细注意到,在我们的 build.dart 文件中,我们放置了 'lib/model/.dart' 和 'lib/。镖'。构建知道通过指定的路径在哪里查找这些文件,并查找包含“某物”部分的文件。因此,在运行 build.dart 文件之前将该行保留在这些文件中非常重要
-
最后,现在我可以使用 main.dart 文件中的序列化器将 json 字符串序列化为我的自定义 dart 对象类 Person。在我的 main.dart 中,我在 initState() 中添加了以下代码
主程序.dart
Person _person;
@override
void initState() {
super.initState();
String json = "{"
"\"name\":\"John\",\"age\":30,\"cars\": "
"["
"{ \"name\":\"Ford\", \"models\":[ \"Fiesta\", \"Focus\", \"Mustang\" ] },"
"{ \"name\":\"BMW\", \"models\":[ \"320\", \"X3\", \"X5\" ] },"
"{ \"name\":\"Fiat\", \"models\":[ \"500\", \"Panda\" ] }"
"]}";
setState(() {
_person = serializers.deserializeWith(
Person.serializer, JSON.decode(json));
});
}
我的示例项目也可以在 Github 上找到建成价值样本项目 https://github.com/toufikzitouni/Built-Value-Example