3

How do I initialise the variables of a super class in a named factory constructor? Here is my sample code:

class ClassA{ final a; final b; ClassA({this.a, this.b}); } class ClassB extends ClassA{ final c; List <String> myList; ClassB({this.c,this.myList}); factory ClassB.fromJson(json){ var list = json["list"] as List; List<String> tempList = []; list.forEach((item)=>tempList.add(item)); return ClassB( c: json["c"], myList: tempList ); } } 

I am not sure how or where exactly do i call the super constructor for Class A so that I can initialise its variables.

1 Answer 1

4

Here is the way to call super:

class ClassA{ final a; final b; ClassA({this.a, this.b}); } class ClassB extends ClassA{ final c; List <String> myList; ClassB({final a, final b, this.c,this.myList}) : super(a: a, b: b); factory ClassB.fromJson(json){ var list = json["list"] as List; List<String> tempList = []; list.forEach((item)=>tempList.add(item)); return ClassB( c: json["c"], myList: tempList ); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.