Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

dart实例初始化时如何在一些额外操作后初始化final成员?

假设有这样的一个类:

class Article {
  final content;
  final info;

  Article({
    this.content,
    this.info
  });

  Article.fromJson(String json) :
    content = jsonDecode(json)['content'],
    info = jsonDecode(json)['info']
  ;
}

这个article类的实例我需要可以从一个json字符串构建,fromJson那里虽然用初始化列表实现了,但是每一次都要重新decode,浪费资源,请问如何在不去掉final的情况下,只decode一次然后初始化content和info?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...