I am not able to use a Future call in a constructor of my controller. I am trying to initialize a list from database on page load, which requires me to use Future.
The value is retrieved using a method declared in the service class, which i have added in the module class just before the controller.
type(DataStreamService);
type(DataStreamController);
Here is the code for my controller:
@NgController(selector: '[data-stream-controller]', publishAs: 'DataStreamCtrl')
class DataStreamController {
List<InputDataStruct> dataStream;
DataStreamController(DataStreamService dataStreamSrvc){
Future<List<InputDataStruct>> getDataStreamFtr = dataStreamSrvc.getDataStream();
getDataStreamFtr.then((value){
dataStream = value;
})
.catchError(print("Error occured"));
}
}
Is using Future in constructor not recommended. If not what am i missing here.
Here is the dataStreamSrvc.getDataStream() code:
Future<List<InputDataStruct>> getDataStream(){
List<InputDataStruct> dataStream = [];
Completer completer = new Completer();
Future getSomeInfoFtr = myDbHelper.getSomeInfo();
getSomeInfoFtr.then((infoAndList){
for(String info in infoAndList){
inputDataStruct = new InputDataStruct(info);
dataStream.add(inputDataStruct);
}
});
completer.complete(dataStream);
return completer.future;
}
not working
? I guess you just accessdataStream
before it actually got a value assigned. Where do you check ifdataStream
has a value? Try to add aprint(value)
inthen
to see if a value is returned. - Günter ZöchbauerdataStreamSrvc.getDataStream()
doesn't return a value. Maybe you forgot areturn
ingetDataStream()
? - Günter Zöchbauer