@@ -7,29 +7,15 @@ class HomePage extends StatefulWidget {
77}
88
99class _HomePageState extends State <HomePage > {
10- var jsonStr = '''[{
10+ var jsonObject = JsonObject . decode ( '''[{
1111 "url": "https://baidu.com",
1212 "name": "Baidu",
13- "time": 1234567890
13+ "time": " 1234567890"
1414 },{
1515 "url": "https://google.com.hk",
1616 "name": "Google",
1717 "time": 1234567890
18- }]''' ;
19- var jsonObject;
20-
21- Widget buildDivider () {
22- return Divider (
23- thickness: 1 ,
24- color: Theme .of (context).primaryColor,
25- );
26- }
27-
28- @override
29- void initState () {
30- super .initState ();
31- jsonObject = JsonObject .fromString (jsonStr);
32- }
18+ }]''' );
3319
3420 @override
3521 Widget build (BuildContext context) {
@@ -40,90 +26,59 @@ class _HomePageState extends State<HomePage> {
4026 body: ListView (
4127 padding: EdgeInsets .all (10 ),
4228 children: [
43- /// get list
29+ /// encode
4430 Text (
4531 "转换成对象后的jsonObject: " ,
4632 style: Theme .of (context).textTheme.headline5,
4733 ),
48- Text (jsonObject.getValue ().runtimeType.toString ()),
49- Text (jsonObject.encodePretty (indent: 4 )),
50-
51- /// get map
52- buildDivider (),
53- Text (
54- "访问jsonObject[0]: " ,
55- style: Theme .of (context).textTheme.headline5,
56- ),
57- Text (jsonObject[0 ].getValue ().runtimeType.toString ()),
58- Text (jsonObject[0 ].encodePretty (indent: 4 )),
59-
60- /// get url
61- buildDivider (),
62- Text (
63- "访问jsonObject[0].url: " ,
64- style: Theme .of (context).textTheme.headline5,
65- ),
66- Text (jsonObject[0 ].url.getValue ().runtimeType.toString ()),
67- Text (jsonObject[0 ].url.encodePretty (indent: 4 )),
68- ElevatedButton (
69- onPressed: () => setState (
70- () => jsonObject[0 ].url = jsonObject[1 ].url.getValue (),
71- ),
72- child: Text ("把百度的url改成谷歌的url" ),
73- ),
34+ Text (jsonObject.valueRuntimeType.toString ()),
35+ Text (JsonObject .encodePretty (jsonObject, indent: 4 )),
7436
75- /// get name
76- buildDivider (),
37+ /// get value
7738 Text (
78- "访问jsonObject[0].name: " ,
39+ "Get方法测试 " ,
7940 style: Theme .of (context).textTheme.headline5,
8041 ),
81- Text (jsonObject[0 ].name.getValue ().runtimeType.toString ()),
82- Text (jsonObject[0 ].name.encodePretty (indent: 4 )),
42+ Text (jsonObject[0 ].getValue ().toString ()),
43+ Text (jsonObject[0 ].url.getValue ().toString ()),
44+ Text (jsonObject[0 ].name.getValue ().toString ()),
8345
84- /// get time
85- buildDivider (),
46+ /// set value
8647 Text (
87- "访问jsonObject[0].time: " ,
48+ "Set方法测试 " ,
8849 style: Theme .of (context).textTheme.headline5,
8950 ),
90- Text (jsonObject[ 0 ].time. getValue ().runtimeType. toString ()),
91- Text (jsonObject[ 0 ].time. encodePretty (indent : 4 )),
92- ElevatedButton (
93- onPressed : () => setState (
94- () => jsonObject[ 0 ].time = 111111111 ,
95- ) ,
96- child: Text ("修改jsonObject[0].time为111111111 " ),
51+ MaterialButton (
52+ onPressed : () {
53+ setState (() {
54+ jsonObject[ 0 ].url = jsonObject[ 1 ].url;
55+ });
56+ } ,
57+ child: Text ("设置百度链接为谷歌链接 " ),
9758 ),
9859
99- /// add key value pairs using dot operator
100- buildDivider (),
60+ /// excecute test
10161 Text (
102- "访问jsonObject[1].stars: " ,
62+ "方法执行能力 " ,
10363 style: Theme .of (context).textTheme.headline5,
10464 ),
105- Text (jsonObject[1 ].stars.getValue ().runtimeType.toString ()),
106- Text (jsonObject[1 ].stars.encodePretty (indent: 4 )),
107- ElevatedButton (
108- onPressed: () => setState (
109- () => jsonObject[1 ].stars = 5 ,
110- ),
111- child: Text ('jsonObject[1].stars = 5' ),
112- ),
113-
114- /// add key value pairs using add()
115- buildDivider (),
116- Text (
117- "访问jsonObject[0].stars: " ,
118- style: Theme .of (context).textTheme.headline5,
65+ Column (
66+ children: jsonObject.getValue ().map <Text >((item) {
67+ final itemObject = JsonObject .fromMap (item);
68+ return Text (itemObject.name.getValue ());
69+ }).toList (),
11970 ),
120- Text (jsonObject[0 ].stars.getValue ().runtimeType.toString ()),
121- Text (jsonObject[0 ].stars.encodePretty (indent: 4 )),
122- ElevatedButton (
123- onPressed: () => setState (
124- () => jsonObject[0 ].add ("stars" , 5 ),
125- ),
126- child: Text ('jsonObject[0].add("stars", 5)' ),
71+ MaterialButton (
72+ onPressed: () {
73+ setState (() {
74+ (jsonObject as JsonObject ).apply <List >((list) {
75+ final listCopy = List .from (list);
76+ list.addAll (listCopy);
77+ return list;
78+ });
79+ });
80+ },
81+ child: Text ("列表长度升为4" ),
12782 ),
12883 ],
12984 ),
0 commit comments