{ "name":"John","age":30,"cars": [ { "name":"Ford","models":[ "Fiesta","Focus","Mustang" ] },{ "name":"BMW","models":[ "320","X3","X5" ] },{ "name":"Fiat","models":[ "500","Panda" ] } ]
}
解决方法
>在我的Flutter项目中,首先我导入了以下库:
dependencies:
built_value: ^1.0.1
built_collection: ^1.0.0dev_dependencies:
build_runner: ^0.3.0
built_value_generator:^1.0.1
>我创建了一个名为tool的文件夹.在其中,我放了2个文件:build.dart和watch.dart.这些文件的实现如下所示
build.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); }
watch.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()]),'lib/*.dart'])),deleteFilesByDefault: true); }
>我创建了一个serializers.dart文件,它将我的json字符串序列化为我的自定义dart对象,以及我的模型对象person.dart
serializers.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并点击“Run build.dart”即可.这将创建2个文件:“person.g.dart”和“serializers.g.dart”.如果您仔细注意,在我们的build.dart文件中,我们将’lib / model / .dart’和’lib / .dart’放在一起.构建通过遍历指定的路径知道在哪里查找这些文件,并查找包含部分“内容”的文件.因此,在运行build.dart文件之前,将该行保留在这些文件中非常重要
>最后,现在我可以在main.dart文件中使用序列化程序将json字符串序列化为我的自定义dart对象类Person.在我的main.dart中,我在initState()中添加了以下代码
main.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 Built value sample project上获得