dart - Can non-trivial constructors call Future returning functions (how or alternatives) -
suppose have:
class schema { schema.fromtext(string jsonstring) { ... } }
in constructor, assume there url provided in jsonstring download data , only api read url 1 returns future. also, assume schema valid object when url data has been read , processed. possible implement ...?
what want not possible standard constructors.
instead, try static method returns new instance wrapped in future.
something like:
class schema { schema._fromapi(string apiresults) { ... } static future<schema> build(string jsonstring) { return getcontentsofurl(jsonstring['url']) .then((contents) => new schema._fromapi(contents)); } }
Comments
Post a Comment