您可能希望有异步工厂构造函数,但是它们
aren't allowed
.
所以一个解决方案是
static getInstance()
,例如:
class Storage {
static Future<Storage> getInstance() async {
String docsFolder = (await getApplicationDocumentsDirectory()).path;
return new Storage(
docsFolder + '/image_cache/',
docsFolder + '/json_cache/',
(await SharedPreferences.getInstance()).getString('UUID'));
}
String imageDirectory;
String jsonDirectory;
String uuid;
Storage(this.imageDirectory, this.jsonDirectory, this.uuid);
}
可以将参数传递到
getInstance
从而根据需要进入构造器。请用以下方式调用上述内容:
Storage s = await Storage.getInstance();