import 'dart:async'; import 'package:flutter/foundation.dart'; /// A mixin class that provides a way to ensure the class is initialized. abstract mixin class Init { bool _isInit = false; final _initCompleter = >[]; /// Ensure the class is initialized. Future ensureInit() async { if (_isInit) { return; } var completer = Completer(); _initCompleter.add(completer); return completer.future; } Future _markInit() async { _isInit = true; for (var completer in _initCompleter) { completer.complete(); } _initCompleter.clear(); } @protected Future doInit(); /// Initialize the class. Future init() async { if (_isInit) { return; } await doInit(); await _markInit(); } }