Advertisement
Rimasi89

Untitled

Apr 6th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'dart:convert';
  3.  
  4. const jsonCatFact =
  5. '{"text": "In an average year, cat owners in the United States spend over 2 billion on cat food.","_id":"591f98803b90f7150a19c229"}';
  6.  
  7. void main() => runApp(MyApp());
  8.  
  9.  
  10. class MyApp extends StatelessWidget {
  11. final myOject = CatFact.fromJson(jsonDecode(jsonCatFact));
  12.  
  13.  
  14. @override
  15. Widget build(BuildContext context) {
  16. return MaterialApp(
  17. home: Scaffold(
  18. appBar: AppBar(
  19. title: const Text('Json Parsing'),
  20. ),
  21. body: Center(
  22. child: Text(myOject.text),
  23. ),
  24. ),
  25. );
  26. }
  27. }
  28.  
  29. // PODO (Plain Old Dart Object)
  30. class CatFact {
  31. String text;
  32. String id;
  33.  
  34. CatFact({this.text, this.id});
  35.  
  36. factory CatFact.fromJson(Map<String, dynamic> parsedJson) {
  37. return CatFact(
  38. id: parsedJson['_id'],
  39. text: parsedJson['text'],
  40. );
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement