-
Notifications
You must be signed in to change notification settings - Fork 74
/
json_annotation.dart
112 lines (101 loc) · 2.95 KB
/
json_annotation.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import 'package:flutter/material.dart';
import 'model/address.dart';
import 'model/person_model.dart';
/// Copy the package name with dev_dependencies
/// `dependencies:`
/// `json_annotation: ^4.4.0`
///
/// `dev_dependencies:`
/// `build_runner: ^2.0.0`
/// `json_serializable: ^6.0.0``
///
/// For help, run this command in your terminal:
/// [$ flutter pub run build_runner --help]
///
/// You can find one simple example here:
/// [https://github.com/google/json_serializable.dart/tree/master/example]
class MyJSONAnnotation extends StatefulWidget {
const MyJSONAnnotation({Key? key}) : super(key: key);
@override
State<MyJSONAnnotation> createState() => _MyJSONAnnotationState();
}
class _MyJSONAnnotationState extends State<MyJSONAnnotation> {
// ignore: prefer_typing_uninitialized_variables
var toJson;
// ignore: prefer_typing_uninitialized_variables
var fromJson;
late PersonModel person;
final TextStyle _textStyle = const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
);
@override
void initState() {
super.initState();
person = PersonModel(
name: 'Md Siam',
creditCardNumber: '1234123412341234',
birthday: DateTime.now(),
address: Address(
city: 'Dhaka',
street: 'Dhaka',
number: 1,
),
);
}
_converter() {
final json = person.toJson();
toJson = json;
debugPrint('toJSON: ${person.toJson()}');
final newPerson = PersonModel.fromJson(json);
fromJson = newPerson;
debugPrint('fromJSON: $newPerson');
}
_resetValue() {
toJson = null;
fromJson = null;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('JSON Annotation')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
alignment: Alignment.center,
height: 300,
width: 350,
color: Colors.purple[100],
child: Column(
children: [
const SizedBox(height: 30),
Text(':::person.toJson():::\n', style: _textStyle),
Text('$toJson', textAlign: TextAlign.center),
const SizedBox(height: 30),
Text(':::PersonModel.fromJson(json):::\n', style: _textStyle),
Text('$fromJson', textAlign: TextAlign.center),
],
),
),
const SizedBox(height: 30),
ElevatedButton(
child: const Text('EXECUTE CONVERSION'),
onPressed: () => setState(() {
_converter();
}),
),
const SizedBox(height: 5.0),
ElevatedButton(
child: const Text('RESET'),
onPressed: () => setState(() {
_resetValue();
}),
),
],
),
),
);
}
}