Flutter / GetX Steps / Step6: Working with multiples Model
Working with Model
-
Steps
1. Basic code for model
1. define model name 'User'
2. define model parameters/variables
3. define constructor with class name
4. define fromJson()
5. define Map()
class User { //1. variable declaration int? userId; int? id; String? title; String? body; //2. constructor User({ this.userId, this.id, this.title, this.body, }); //3. fromJson() User.fromJson(Map json) { id = json['id']; userId = json['userId']; title = json['title']; body = json['body']; } //4. Map() Map toJson() { final Map data = new Map (); data['userId'] = this.userId; data['id'] = this.id; data['title'] = this.title; data['body'] = this.body; return data; } } 2. use model in the controller
import user model
import 'package:getx_restaurant/models/user.dart'; create object of User model
User? user; set the data to model using API
try { isLoading(true); http.Response response = await http.get(Uri.tryParse( 'https://jsonplaceholder.typicode.com/posts/1')!); if (response.statusCode == 200) { ///data successfully var result = jsonDecode(response.body); user = User.fromJson(result); } else { print('error fetching data'); } } catch (e) { print('Error while getting data is $e'); } finally { isLoading(false); } complete code
import 'package:flutter/cupertino.dart'; import 'dart:convert'; import 'package:get/get.dart'; import 'package:getx_restaurant/models/user.dart'; import 'package:http/http.dart' as http; class LoginController extends GetxController { final name="manoj"; var isLoading = false.obs; User? user; @override void onInit() { super.onInit(); fetchData(); } fetchData() async { try { isLoading(true); http.Response response = await http.get(Uri.tryParse( 'https://jsonplaceholder.typicode.com/posts/1')!); if (response.statusCode == 200) { ///data successfully var result = jsonDecode(response.body); user = User.fromJson(result); //print(user?title); } else { print('error fetching data'); } } catch (e) { print('Error while getting data is $e'); } finally { isLoading(false); } } } Access the data from view
child: Text("${logincontroller.user?.title}"), -
2. Multiple Models
class Geo { final double lat; final double lng; Geo({ this.lat = 0.0, this.lng = 0.0, }); Map toJson() { return { 'lat': lat, 'lng': lng, }; } } class Address { final String? street; final String? suite; final String? city; final String? zipcode; Address({ this.street, this.suite, this.city, this.zipcode, }); Map toJson() { return { 'street': street, 'suite': suite, 'city': city, 'zipcode': zipcode, }; } } class Company { final String? name; final String? catchPhrase; final String? bs; Company({ this.name, this.catchPhrase, this.bs, }); Map toJson() { return { 'name': name, 'catchPhrase': catchPhrase, 'bs': bs, }; } } class User { final int id; final String? username; final String? email; final Address? address; final String? phone; final String? website; final Company? company; User({ required this.id, this.username, this.email, this.address, this.phone, this.website, this.company, }); Map toJson() { return { 'id': id, 'username': username, 'email': email, 'address': address?.toJson(), 'phone': phone, 'website': website, 'company': company?.toJson(), }; } }