Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'dart:convert';
- const jsonCatFact =
- '{"text": "In an average year, cat owners in the United States spend over 2 billion on cat food.","_id":"591f98803b90f7150a19c229"}';
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- final myOject = CatFact.fromJson(jsonDecode(jsonCatFact));
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- home: Scaffold(
- appBar: AppBar(
- title: const Text('Json Parsing'),
- ),
- body: Center(
- child: Text(myOject.text),
- ),
- ),
- );
- }
- }
- // PODO (Plain Old Dart Object)
- class CatFact {
- String text;
- String id;
- CatFact({this.text, this.id});
- factory CatFact.fromJson(Map<String, dynamic> parsedJson) {
- return CatFact(
- id: parsedJson['_id'],
- text: parsedJson['text'],
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement