如何在Dart上准备好对象时调度事件?

Fer*_*ndo 3 dart dart-html dart-async

我在Dart中尝试了一些WebGL,我创建了一个从单独的文件加载着色器的类,我想在对象准备就绪时抛出一个事件(函数),所以我可以继续我的应用程序,因为我知道我的着色器已正确加载.有人知道一个简单的方法吗?

Joh*_*ans 6

一种方法是使用Future模式来实现这一目标:

Future<SomeType> initMyObject(){
   final c = new Completer();

   // Do my object init stuff
   // and when it is complete:
   c.complete(instanceOfSomeType);

   // Return the Future object to any subscribers.
   return c.future;
}
Run Code Online (Sandbox Code Playgroud)

然后在其他地方你可以得到这样的通知:

initMyObject().then((SomeType t){
   //executes when future completes
});
Run Code Online (Sandbox Code Playgroud)