Suppose you have:
class Schema {
Schema.fromText(String jsonString) {
...
}
}
In this constructor, assume there is an URL provided in the jsonString to download data and the only API to read an URL is one that returns a Future. Also, assume Schema is only a valid object when that URL data has been read and processed. Is it possible to even implement ...?
What you want to do is not possible with standard constructors.
Instead, try a static method that returns a new instance wrapped in a 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));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With