Flutter: inheritance, what i need know and how, an implemented method in parent that i need to use it in child and add more implementation on it
251
It's a little hard to understand what you need, but how about this:
@override
Map<String, dynamic> toJson() {
final data = super.toJson();
data['Gov'] = gov.toJson();
return data;
}
Author by
Ahmad Mohy
Updated on November 28, 2022Comments
-
Ahmad Mohy over 1 year
- This is the parent which has an implemented method
abstract class CommonModel { int id; String name; .... Map<String, dynamic> toJson(){ final Map<String, dynamic> data = new Map<String, dynamic>(); data['Id'] = this.id; data['Name'] = this.name; return data; } }
- and this the the child class which i need to use the method [toJson] on it with its implementation and add more data.
import 'package:Elnota/models/governorate.dart'; import 'package:Elnota/models/abstract_common_model.dart'; class Centeral extends CommonModel{ Governorate gov; Centeral({ int id, String name, this.gov}) : super(id:id, name:name); Centeral.fromJson(Map<String, dynamic> json) :super.fromJson(json){ gov = json['Gov'] != null ? new Governorate.fromJson(json['Gov']) : null; } Map<String, dynamic> toJson() { final Map<String, dynamic> data = new Map<String, dynamic>(); data['Gov'] = this.gov; } }
-
Ahmad Mohy about 3 yearsyes, that's what i need to know, how to override an implemented method in the child from the parent class
-
nvoigt about 3 yearsThere is nothing more to do. Just name it the same. Done. The
@override
attribute is entirely optional and only used to give you a warning, should the super class decide to delete or rename the method.