Last active
July 21, 2022 13:53
-
-
Save jlamimoso/fc430716febbc856e708023aabf48fba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
void main() { | |
// jsonSemList(); | |
jsonComList(); | |
// lm.m!.add(Msg('3333')); | |
// lmJson = jsonEncode(lm); | |
// print(lmJson); | |
} | |
void testeDate() { | |
var ts = DateTime.now().millisecondsSinceEpoch; | |
var d = DateTime.fromMillisecondsSinceEpoch(ts); | |
print('dia: ${d.day} hora: ${d.hour}:${d.minute}:${d.second}'); | |
} | |
void jsonSemList() { | |
String js = jsonEncode(Msg('111')); | |
print('encode: $js'); | |
Map<String, dynamic> m = jsonDecode(js); | |
print('decode: ${m['x']}'); | |
} | |
void jsonComList() { | |
List<Msg> l = []; | |
l.add(Msg('1111')); | |
l.add(Msg('2222')); | |
ListMsg lm = ListMsg(l); | |
var lmJson = jsonEncode(lm); | |
print(lmJson); | |
Map<String, dynamic> map = jsonDecode(lmJson); | |
List<dynamic> l1 = map['xl']; | |
var s = Msg('3333').toJson(); | |
l1.add(s); | |
l.clear(); | |
for (Map<String, dynamic> x in l1) { | |
Msg m = Msg(x['x']); | |
l.add(m); | |
} | |
for (Msg m in lm.m!) { | |
print('msg-> ${m.x}'); | |
} | |
print(jsonEncode(lm)); | |
} | |
class Msg { | |
final String x; | |
Msg(this.x); | |
Map<String, dynamic> toJson() => { 'x': x }; | |
Msg.fromJson(Map<String, dynamic> json) | |
: x = json['x']; | |
} | |
class ListMsg { | |
final List<Msg>? m; | |
ListMsg(this.m); | |
Map<String, dynamic> toJson() => { 'xl': m }; | |
ListMsg.fromJson(Map<String, dynamic> json) | |
: m = json['xl'] != null ? List<Msg>.from(json['xl']) : null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment