build model class to map to/from firestore - flutter

I'm trying to create classes to match my firestore schema. My schema in firestore is
docID
------------>↓location //Map
↓country //Map
name: "Ireland"
code: "IRE"
↓suburb //Map
name: "Cork"
code: "crk"
↓postcode //Map
name: "4"
code: "4"
I'm a little confused how to model the above. This is what I've done so far..
class VoterData {
VoterData({this.location});
final Location location;
Map<String, dynamic> toMap() {
return {
'location': location,
};
}
factory VoterData.fromMap(Map<String, dynamic> map) {
return VoterData(location: map['location']);
}
}
class Location {
final Suburb suburb;
final Postcode postcode;
final Country country;
Location({this.suburb, this.country, this.postcode});
factory Location.fromJson(Map<String, dynamic> json) => Location(
suburb: json['suburb'],
postcode: json['postcode'],
country: json['country'],
);
Map<String, dynamic> toJson() => {
"suburb": suburb,
"postcode": postcode,
"country": country,
};
}
class Suburb {
String code;
String name;
Suburb({
this.code,
this.name,
});
factory Suburb.fromJson(Map<String, dynamic> json) => Suburb(
code: json["code"],
name: json["name"],
);
Map<String, dynamic> toJson() => {
"code": code,
"name": name,
};
}
class Postcode {
String code;
String name;
Postcode({
this.code,
this.name,
});
factory Postcode.fromJson(Map<String, dynamic> json) => Postcode(
code: json["code"],
name: json["name"],
);
Map<String, dynamic> toJson() => {
"code": code,
"name": name,
};
}
class Country {
String code;
String name;
Country({
this.code,
this.name,
});
factory Country.fromJson(Map<String, dynamic> json) => Country(
code: json["code"],
name: json["name"],
);
Map<String, dynamic> toJson() => {
"code": code,
"name": name,
};
}
I'll looking for my firestore data to be strongly typed to my object model. Suppose I need to write a suburb object to firestore. How would I design my model class ..?

Hi I recommend you use like this class
class DocumentModel {
String collection;
String documentID;
Map<String, dynamic> data;
DataBaseModel(this.collection);
}
class VoterModel extends DocumentModel {
Map<String, dynamic> data = {
'country': { 'name':'', 'code':''},
'suburb': { 'name':'', 'code':'' },
'postcode' : { 'name':'', 'code':'' }
}
VoterModel() : super('<COLLECTION NAME>');
}
so when you get a QuerySnapshot you can parse like this
List<VoterModel> list = querySnapshot.documents.map((DocumentSnapshot docSnapshot) {
VoterModel voter = new VoterModel();
voter.documentID = docSnapshot.documentID;
voter.data = docSnapshot.data;
return voter;
}).toList();
Access to data
list.last.data['country']['name']

Related

_TypeError (type '(dynamic) => FutureMatch' is not a subtype of type '(String, dynamic) =>

hello can anyone help me to fix this error
error coming from FutureMatch.dart
from line
List<FutureMatch> futureMatchFromJson(String str) => List<FutureMatch>.from(json.decode(str).map((x) => FutureMatch.fromJson(x)));
**Exception has occurred. _TypeError (type '(dynamic) => FutureMatch' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform')**
and my FutureMatch.dart code
`
// To parse this JSON data, do
// final futureMatch = futureMatchFromJson(jsonString);
import 'dart:convert';
List<FutureMatch> futureMatchFromJson(String str) => List<FutureMatch>.from(json.decode(str).map((x) => FutureMatch.fromJson(x)));
String futureMatchToJson(List<FutureMatch> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class FutureMatch {
FutureMatch({
this.fixture,
this.league,
this.teams,
this.goals,
this.score,
});
Fixture? fixture;
League? league;
Teams? teams;
Goals? goals;
Score? score;
factory FutureMatch.fromJson(Map<String, dynamic> json) => FutureMatch(
fixture: Fixture.fromJson(json["fixture"]),
league: League.fromJson(json["league"]),
teams:Teams.fromJson(json["teams"]),
goals: Goals.fromJson(json["goals"]),
score: Score.fromJson(json["score"]),
);
Map<String, dynamic> toJson() => {
"fixture": fixture!.toJson(),
"league": league!.toJson(),
"teams": teams!.toJson(),
"goals": goals!.toJson(),
"score": score!.toJson(),
};
}
class Teams {
Teams({
this.home,
this.away,
});
dynamic home;
dynamic away;
factory Teams.fromJson(Map<String, dynamic> json) => Teams(
home: json["home"],
away: json["away"],
);
Map<String, dynamic> toJson() => {
"home": home,
"away": away,
};
}
class Fixture {
Fixture({
this.id,
this.referee,
this.timezone,
this.date,
this.timestamp,
this.periods,
this.venue,
this.status,
});
int? id;
dynamic referee;
String? timezone;
DateTime? date;
int? timestamp;
Periods? periods;
Venue? venue;
Status? status;
factory Fixture.fromJson(Map<String, dynamic> json) => Fixture(
id: json["id"],
referee: json["referee"],
timezone: json["timezone"],
date: DateTime.parse(json["date"]),
timestamp: json["timestamp"],
periods: Periods.fromJson(json["periods"]),
venue: Venue.fromJson(json["venue"]),
status: Status.fromJson(json["status"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"referee": referee,
"timezone": timezone,
"date": date!.toIso8601String(),
"timestamp": timestamp,
"periods": periods!.toJson(),
"venue": venue!.toJson(),
"status": status!.toJson(),
};
}
class Periods {
Periods({
this.first,
this.second,
});
dynamic first;
dynamic second;
factory Periods.fromJson(Map<String, dynamic> json) => Periods(
first: json["first"],
second: json["second"],
);
Map<String, dynamic> toJson() => {
"first": first,
"second": second,
};
}
class Status {
Status({
this.long,
this.short,
this.elapsed,
});
String? long;
String? short;
dynamic elapsed;
factory Status.fromJson(Map<String, dynamic> json) => Status(
long: json["long"],
short: json["short"],
elapsed: json["elapsed"],
);
Map<String, dynamic> toJson() => {
"long": long,
"short": short,
"elapsed": elapsed,
};
}
class Venue {
Venue({
this.id,
this.name,
this.city,
});
dynamic id;
dynamic name;
dynamic city;
factory Venue.fromJson(Map<String, dynamic> json) => Venue(
id: json["id"],
name: json["name"],
city: json["city"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"city": city,
};
}
class Goals {
Goals({
this.home,
this.away,
});
int? home;
int? away;
factory Goals.fromJson(Map<String, dynamic> json) => Goals(
home: json["home"],
away: json["away"],
);
Map<String, dynamic> toJson() => {
"home": home,
"away": away,
};
}
class Away {
Away({
this.id,
this.name,
this.logo,
this.winner,
});
int? id;
String? name;
String? logo;
dynamic winner;
factory Away.fromJson(Map<String, dynamic> json) => Away(
id: json["id"],
name: json["name"],
logo: json["logo"],
winner: json["winner"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"logo": logo,
"winner": winner,
};
}
class League {
League({
this.id,
this.name,
this.country,
this.logo,
this.flag,
this.season,
this.round,
});
int? id;
String? name;
String? country;
String? logo;
String? flag;
int? season;
String? round;
factory League.fromJson(Map<String, dynamic> json) => League(
id: json["id"],
name: json["name"],
country: json["country"],
logo: json["logo"],
flag: json["flag"],
season: json["season"],
round: json["round"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"country": country,
"logo": logo,
"flag": flag,
"season": season,
"round": round,
};
}
class Score {
Score({
this.halftime,
this.fulltime,
this.extratime,
this.penalty,
});
Goals? halftime;
Goals? fulltime;
Goals? extratime;
Goals? penalty;
factory Score.fromJson(Map<String, dynamic> json) => Score(
halftime: Goals.fromJson(json["halftime"]),
fulltime: Goals.fromJson(json["fulltime"]),
extratime: Goals.fromJson(json["extratime"]),
penalty: Goals.fromJson(json["penalty"]),
);
Map<String, dynamic> toJson() => {
"halftime": halftime!.toJson(),
"fulltime": fulltime!.toJson(),
"extratime": extratime!.toJson(),
"penalty": penalty!.toJson(),
};
}
`
constent.dart
`String mainurl = "https://v3.football.api-sports.io";
String token = "###########";
`
what i can do to fix this error ?
if you need more information please write .

I got an error while getting data from the API, how should I get the data if flutter?

I am getting data from an API using a model. But I ran into a problem that when I get the 'gallery' data, I get an error, that is, I get the data incorrectly. I need to get the 'gallery' field and inside it take the 'url' field - a link to the photo, in order to use it in the future. Can you tell me how to get the 'url' field correctly?
{
"data": {
"id": 35,
"picture_url": null,
"email_confirmed": false,
"gallery": [
{
"url": "https://picture-staging.s3.eu-central.jpeg",
"mime_type": "image/jpeg",
"type": "gallery",
"updated_at": "2022",
"created_at": "2022"
}
],
"updated_at": "2022",
"created_at": "2022"
}
}
model
class User {
final int id;
List? gallery;
User({
required this.id,
this.gallery,
});
User.fromJson(Map<String, dynamic> json)
: this(
id: json['id'] as int,
gallery: json['gallery']['url'],
);
In your API response, there is a list of gallery objects therefore you have to traverse through all of them.
User.fromJson(Map<String, dynamic> json) {
json = json['data'];
id = json['id'];
pictureUrl = json['picture_url'];
emailConfirmed = json['email_confirmed'];
if (json['gallery'] != null) {
gallery = <Gallery>[];
json['gallery'].forEach((v) {
gallery!.add(new Gallery.fromJson(v));
});
}
updatedAt = json['updated_at'];
createdAt = json['created_at'];
}
There are multiple tools that helps you create that .fromJson method, like this. Paste your json there and it will generate dart code for you, really helps me.
The usage should like this:
User user = User.fromJson(yourApiResponseJson);
print(user.id);
print(user.gallery); //prints entire list of gallery
print(user.gallery.first.url); //prints only first object url
I hope that is not your whole model, because that model is not accessing the "data" key on the json response, your model should start getting the key data then pass it to another class that in this case should be named User
here is a brief example
class User {
User({
required this.data,
});
final Data data;
factory User.fromJson(Map<String, dynamic> json) => User(
data: Data.fromJson(json["data"]),
);
}
The Data class could be like this:
class Data {
Data({
required this.id,
required this.pictureUrl,
required this.emailConfirmed,
required this.gallery,
required this.updatedAt,
required this.createdAt,
});
final int id;
final dynamic pictureUrl;
final bool emailConfirmed;
final List<Gallery> gallery;
final String updatedAt;
final String createdAt;
factory Data.fromJson(Map<String, dynamic> json) => Data(
id: json["id"],
pictureUrl: json["picture_url"],
emailConfirmed: json["email_confirmed"],
gallery: List<Gallery>.from(json["gallery"].map((x) => Gallery.fromJson(x))),
updatedAt: json["updated_at"],
createdAt: json["created_at"],
);
}
I reccomend you using Quicktype
Hey you can use this tool to generate your dart model from json.
Below is generated code from above tool
// final user = userFromJson(jsonString);
import 'dart:convert';
User userFromJson(String str) => User.fromJson(json.decode(str));
String userToJson(User data) => json.encode(data.toJson());
class User {
User({
required this.data,
});
Data data;
factory User.fromJson(Map<String, dynamic> json) => User(
data: Data.fromJson(json["data"]),
);
Map<String, dynamic> toJson() => {
"data": data.toJson(),
};
}
class Data {
Data({
this.id,
this.pictureUrl,
this.emailConfirmed,
this.gallery,
this.updatedAt,
this.createdAt,
});
int? id;
String? pictureUrl;
bool? emailConfirmed;
List<Gallery>? gallery;
String? updatedAt;
String? createdAt;
factory Data.fromJson(Map<String, dynamic> json) => Data(
id: json["id"],
pictureUrl: json["picture_url"],
emailConfirmed: json["email_confirmed"],
gallery: List<Gallery>.from(json["gallery"].map((x) => Gallery.fromJson(x))),
updatedAt: json["updated_at"],
createdAt: json["created_at"],
);
Map<String, dynamic> toJson() => {
"id": id,
"picture_url": pictureUrl,
"email_confirmed": emailConfirmed,
"gallery": List<dynamic>.from(gallery.map((x) => x.toJson())),
"updated_at": updatedAt,
"created_at": createdAt,
};
}
class Gallery {
Gallery({
this.url,
this.mimeType,
this.type,
this.updatedAt,
this.createdAt,
});
String? url;
String? mimeType;
String? type;
String? updatedAt;
String? createdAt;
factory Gallery.fromJson(Map<String, dynamic> json) => Gallery(
url: json["url"],
mimeType: json["mime_type"],
type: json["type"],
updatedAt: json["updated_at"],
createdAt: json["created_at"],
);
Map<String, dynamic> toJson() => {
"url": url,
"mime_type": mimeType,
"type": type,
"updated_at": updatedAt,
"created_at": createdAt,
};
}
// You can use like this
final user = userFromJson(jsonString);
String? url = user.data?.gallery?.url;

Flutter error || Unhandled Exception: type 'SearchModel' is not a subtype of type 'List<SearchModel>' in type cast

I am making a search in ListView.builder where data is coming in JSON data
I am not getting the error what it wants to say the searchmodel is not a subtype of type List.
or should I go with another approach to work with search in listview.builder
here is my model
import 'dart:convert';
SearchModel searchModelFromJson(String str) =>
SearchModel.fromJson(json.decode(str));
String searchModelToJson(SearchModel data) => json.encode(data.toJson());
class SearchModel {
SearchModel({
required this.data,
required this.meta,
});
final List<Datum> data;
final Meta meta;
factory SearchModel.fromJson(Map<String, dynamic> json) => SearchModel(
data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
meta: Meta.fromJson(json["meta"]),
);
Map<String, dynamic> toJson() => {
"data": List<dynamic>.from(data.map((x) => x.toJson())),
"meta": meta.toJson(),
};
}
class Datum {
Datum({
required this.id,
required this.attributes,
});
int? id;
Attributes? attributes;
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
id: json["id"],
attributes: Attributes.fromJson(json["attributes"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"attributes": attributes?.toJson(),
};
}
class Attributes {
Attributes({
required this.name,
required this.mobile,
required this.createdAt,
required this.updatedAt,
required this.shopEmail,
required this.shopUniqueId,
});
String? name;
String? mobile;
String? createdAt;
String? updatedAt;
String? shopEmail;
String? shopUniqueId;
factory Attributes.fromJson(Map<String, dynamic> json) => Attributes(
name: json["name"],
mobile: json["mobile"],
createdAt: json["createdAt"],
updatedAt: json["updatedAt"],
shopEmail: json["shopEmail"],
shopUniqueId: json["shopUniqueId"],
);
Map<String, dynamic> toJson() => {
"name": name,
"mobile": mobile,
"createdAt": createdAt,
"updatedAt": updatedAt,
"shopEmail": shopEmail,
"shopUniqueId": shopUniqueId,
};
}
class Meta {
Meta();
factory Meta.fromJson(Map<String, dynamic> json) => Meta(
);
Map<String, dynamic> toJson() => {
};
}
here is my controller
class FetchSearch {
static Future<SearchModel> getUserList(String? query) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? jwt = prefs.getString('jwt');
var response = await http.get(
Uri.parse(searchUrl),
headers: <String,String> {
'Authorization' : 'Bearer $jwt'
},
);
if (response.statusCode == 200) {
print(response.statusCode);
var stringResponse = response.body;
print(stringResponse);
return SearchModel.fromJson(jsonDecode(stringResponse));
// Map<String,dynamic> search = json.decode(response.body);
// print(search);
// List<dynamic> data = map["data"];
// return search.map((json) => Attributes.fromJson(json)).toList();
}
else {
throw Exception();
}
and here is my variables and init function
int _currentPage = 0, _index = 0;
List<SearchModel> searchItem = [];
String query = '';
Future init() async {
final users = await FetchSearch.getUserList(query);
setState(() => this.searchItem = users as List<SearchModel>);
}

Function expressions can't be named. doc error

Hello in my flutter project i got this problem when i want to recall users by thier ID.
Future<UserModel> getUserById(String id)=> _firestore.collection(collection).doc(id){
print("==========id is $id=============");
debugPrint("==========NAME is ${doc.data()['name']}=============");
debugPrint("==========NAME is ${doc.data()['name']}=============");
it gives an error on [ .doc(id){ ]
what shall i do?
an also in my order page it gives same error somehow
_firestore.collection(collection).doc(id).setData()({
"userId": userId,
"cart": convertedCart,
"id": id,
"total": totalPrice,
"createdAt": DateTime.now().millisecondsSinceEpoch,
"description": description,
"status": status
});
}
what do you guys think?
in that line
_firestore.collection(collection).doc(id).setData()({
setData is error
my flutter version is 2.5.1
Try the code below.
Future<UserModel> getUserById(String id){
return FirebaseFirestore.instance.collection("collectionPath")
.doc(id).get().then((doc) => UserModel.fromSnapShot(doc.data()));
}
Your fromSnapshot don't return a UserModel. It should look like this.
import 'package:flutter/foundation.dart';
#immutable
class UserModel{
final String id;
final String name;
final String email;
final String stripeId;
final List<CartItemModel> cart;
final int totalCartPrice;
const UserModel({required this.id, required this.name, required this.email, required this.stripeId, required this.cart,
required this.totalCartPrice});
Map<String, dynamic> toMap() {
return {
'id': id,
'name': name,
'email': email,
'stripeId': stripeId,
'cart': cart,
'totalCartPrice': totalCartPrice,
};
}
factory UserModel.fromMap(Map<String, dynamic> map) {
return UserModel(
id: map['id'] as String,
name: map['name'] as String,
email: map['email'] as String,
stripeId: map['stripeId'] as String,
cart: (map['cart'] as List).map((cart) => CartItemModel.fromMap(cart)).toList() ,
totalCartPrice: map['totalCartPrice'] as int,
);
}
}
#immutable
class CartItemModel{
final String id;
const CartItemModel({required this.id});
Map<String, dynamic> toMap() {
return {
'id': id,
};
}
factory CartItemModel.fromMap(Map<String, dynamic> map) {
return CartItemModel(
id: map['id'] as String,
);
}
}

List array of array in class model flutter

How to create class model in flutter if List Array of Array have 2 type data
Example
"value" : [["Water Melon", "Apple", 10, 23]]
#JsonSerializable()
class GetReportBodyData {
String type;
String title;
List<String> headers;
List<List<int>> value = new List<List<int>>();
GetReportBodyData({this.type, this.title, this.headers, this.value});
#override
String toString() {
return 'GetReportBodyData{type: $type, title: $title, header: $headers, value: $value}';
}
factory GetReportBodyData.fromJson(Map<String, dynamic> json) {
return GetReportBodyData(
type: json["type"],
title: json["title"],
headers: json["header"] != null ? List<String>.from(json["header"]) :[],
value: List<List<int>>.from(json["value"])
);
}
Map<String, dynamic>toJson() => {
"type": type,
"title": title,
"header": headers,
"value": value
};
}
class example {
List<List> value;
example({this.value});
example.fromJson(Map<String, dynamic> json) {
if (json['value'] != null) {
value = new List<List>();
json['value'].forEach((v) { value.add(new List.fromJson(v)); });
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.value != null) {
data['value'] = this.value.map((v) => v.toJson()).toList();
}
return data;
}
}
class Value {
Value({});
Value.fromJson(Map<String, dynamic> json) {
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
return data;
}