Skip to content

Instantly share code, notes, and snippets.

@filiph
Created June 11, 2020 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filiph/939c601164ca1daabd89d3dffe5ba5e7 to your computer and use it in GitHub Desktop.
Save filiph/939c601164ca1daabd89d3dffe5ba5e7 to your computer and use it in GitHub Desktop.
A convoluted viscosity late initialization example
class Viscosity {}
class Material {
Future<Viscosity> computeViscosity() async {
return Viscosity();
}
}
class Goo {
late Viscosity v;
late Future<bool> isReady;
Goo(Material m) {
isReady = init(m);
}
Future<bool> init(Material m) async {
v = await m.computeViscosity();
return true;
}
}
Future<void> main() async {
var m = Material();
var goo = Goo(m);
// If you remove this line, the code breaks at runtime
// with "Field 'v' has not been initialized".
await goo.isReady;
print(goo.v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment