Introduction
Model classes help you organize data from JSON into easy-to-use objects in your app.
Model classes help you organize data from JSON into easy-to-use objects in your app.
class ModelName { final Type propertyName; ModelName({required this.propertyName}); factory ModelName.fromJson(Map<String, dynamic> json) { return ModelName( propertyName: json['propertyName'], ); } Map<String, dynamic> toJson() { return { 'propertyName': propertyName, }; } }
The fromJson factory converts JSON map to a Dart object.
The toJson method converts the Dart object back to JSON map.
class User { final String name; final int age; User({required this.name, required this.age}); factory User.fromJson(Map<String, dynamic> json) { return User( name: json['name'], age: json['age'], ); } Map<String, dynamic> toJson() { return { 'name': name, 'age': age, }; } }
class Product { final String id; final double price; Product({required this.id, required this.price}); factory Product.fromJson(Map<String, dynamic> json) { return Product( id: json['id'], price: (json['price'] as num).toDouble(), ); } Map<String, dynamic> toJson() { return { 'id': id, 'price': price, }; } }
This Flutter app shows how to convert a JSON string into a User object and display its data on screen.
import 'dart:convert'; import 'package:flutter/material.dart'; class User { final String name; final int age; User({required this.name, required this.age}); factory User.fromJson(Map<String, dynamic> json) { return User( name: json['name'], age: json['age'], ); } Map<String, dynamic> toJson() { return { 'name': name, 'age': age, }; } } void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { const jsonString = '{"name": "Alice", "age": 30}'; final Map<String, dynamic> userMap = json.decode(jsonString); final user = User.fromJson(userMap); return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('User Info')), body: Center( child: Text('Name: ${user.name}, Age: ${user.age}'), ), ), ); } }
Always check JSON keys exist before using them to avoid errors.
Use toDouble() when JSON numbers can be int or double.
Model classes make your code easier to maintain and test.
Model classes convert JSON data into Dart objects and back.
Use fromJson factory and toJson method.
This helps keep your app code clean and organized.
fromJson in a Dart model class?print(user.name); output?
class User {
final String name;
User(this.name);
factory User.fromJson(Map json) {
return User(json['name']);
}
}
void main() {
final json = {'name': 'Alice'};
final user = User.fromJson(json);
print(user.name);
}class Product {
final int id;
final String name;
Product(this.id, this.name);
factory Product.fromJson(Map json) {
return Product(json['id'], json['title']);
}
}[{'id':1,'name':'Bob'},{'id':2,'name':'Eve'}]. How do you convert this list into a Dart list of User objects using a model class with fromJson factory?Pass the test and this pattern moves unknown