Skip to content

Instantly share code, notes, and snippets.

@MwBakker
Last active November 29, 2022 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MwBakker/f018d853adc5741fdb94d4598a7ababb to your computer and use it in GitHub Desktop.
Save MwBakker/f018d853adc5741fdb94d4598a7ababb to your computer and use it in GitHub Desktop.
import 'dart:convert';
void main() {
final stringifiedJson = '[{"id":"4","name":"Tulpenrallye 2022","location":"50.1213479,8.4964818","description":"Nog maar vijf weken geleden stonden wij aan de finish van de 67eTulpenrallye. Vandaag word jij door ons uitgenodigd voor 68e Tulpenrallye. Hiermee gaan wij een nieuw hoofdstuk toevoegen aan de lange Tulpenrallye geschiedenis. De 68e Tulpenrallye start op maandagochtend 9 mei 2022 in de buurt van Frankfurt in Duitsland. Na een overnachting in Karlsruhe gaat de rally daarna verder in het vertrouwde Frankrijk. Natuurlijk doen we ook Luxemburg en Belgi? aan in deze editie.","website":"tulpenrallye.nl","email":"rallyoffice@tulpenrallye.nl","phone":" +31 (0) 6 41291219","img":"https:\/\/www.tulpenrallye.nl\/wp-content\/uploads\/2021\/11\/VISUAL-2022-LR-1.jpg","vehicle_date_from":"1899-01-01","vehicle_date_to":"1973-01-01","vehicle_type_id":"1","user_id":null,"vehicle_region_id":"1","brand":null,"model":null,"event_id":"4","date":"2023-05-09","begin":"09:00:00","end":"16:00:00","is_brand_related":"0","vehicle_types":["1"],"vehicle_regions":["12","1","2","3","4","5","6"],"dates":[{"date":"2023-05-09","begin":"09:00:00","end":"16:00:00"}]}]';
final json = jsonDecode(stringifiedJson)[0];
final event = Event.fromJson(json);
print('id: ${event.id}');
print('name: ${event.name}');
print('website: ${event.website}');
}
abstract class Automotive {
int? id;
String? name;
String? description;
String? pic;
Automotive({
required id,
required name,
required description,
required pic,
});
}
abstract class AutomotiveLocationBased extends Automotive {
String? website;
String? phone;
String? email;
String? place, street, houseNumber;
List<double>? location;
String? distance;
AutomotiveLocationBased({
required this.website,
required this.email,
required this.phone,
required this.location,
this.place,
this.street,
this.distance,
this.houseNumber,
required super.id,
required super.name,
required super.description,
required super.pic,
});
}
class Event extends AutomotiveLocationBased {
Event({
required super.id,
required super.name,
required super.location,
required super.description,
required super.website,
required super.email,
required super.phone,
required super.pic,
});
factory Event.fromScratch() {
return Event(
id: null,
name: null,
description: null,
pic: null,
location: null,
website: null,
email: null,
phone: null);
}
factory Event.fromJson(Map<String, dynamic> json) {
return Event(
id: json['id'],
name: json['name'],
description: json['description'],
website: json['website'],
email: json['email'],
location: List<double>.from(json['location'].split(',').map((data) => double.parse(data))),
phone: json['phone'],
pic: json['img'],
);
}
Map<String, dynamic> toJson() {
return {
'name': name,
'description': description,
'pic': pic,
'location': location,
'website': website,
'email': email,
'phone': phone,
};
}
}
/// this does work
abstract class Car {
int? id;
String? name;
String? description;
Car({
required this.id,
required this.name,
required this.description,
});
}
abstract class CarCombustedEngine extends Car {
String? fuelType;
CarCombustedEngine({
required id,
required name,
required description,
required this.fuelType
}) : super(
id: id,
name: name,
description: description);
}
class Cabrio extends CarCombustedEngine {
String? roofMaterial;
String? someOtherProp;
Cabrio({
required id,
required name,
required description,
required fuelType,
required this.roofMaterial,
required this.someOtherProp
}) : super(
id: id,
name: name,
description: description,
fuelType: fuelType);
factory Cabrio.fromJson(Map<String, dynamic> json) =>
Cabrio(
id: int.parse(json['id']),
name: json['name'],
fuelType: json['fuel_type'],
description: json['description'],
roofMaterial: json['roof_material'],
someOtherProp: json['some_other_prop']
);
}
// final stringifiedJsonn = '{"id": "1", "name": "ford", "description": "fast car", "roof_material": "glass", "fuel_type": "gasoline", "some_other_prop": "hello"}';
// final jsonn = jsonDecode(stringifiedJsonn);
// final cabrio = Cabrio.fromJson(json);
// print('Roof material: ${cabrio.roofMaterial}'); // null
// print('Some other prop: ${cabrio.someOtherProp}');
// print('Fueltype: ${cabrio.fuelType}');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment