Reputation: 355
I have following code :
class Article {
String title;
String contentString;
Content content;
Article(String title, String contentString) {
this.title = title;
this.content = contentString;
//I want to call function here
}
Article.fromJson(Map<String, dynamic> json)
: title = json['title'],
contentString = json['content'];
void _someFunction() {
//create function to generate the Content
}
}
class Content {
final String text;
final bool isLink;
Content({this.text, this.isLink});
}
What i want to achieve is calling a function when the class is initialized. I tried to debug and add break point but it's not triggered. Is it possible to call a function when the class is initialized?
Upvotes: 0
Views: 338
Reputation: 71623
Any code you put at the //I want to...
point should be called when the Article
constructor is called. It won't be called if the Article.fromJson
constructor is called instead. I'm guessing that's what's happening since you haven't said how you constrcut the Article
s.
Consider changing the fromJson
constructor to:
Article.fromJson(Map<String, dynamic> json)
: this(json['title'], json['content']);
This is a redirecting generative constructor, which means it forwards to the Article
constructor, and all code run by that will also be run when using Article.fromJson
.
Upvotes: 1