I'm trying to execute different fetch based on data type but I'm always getting same data. So here is the code where I'm fetching data:
Future.delayed(Duration.zero, () async {
if (widget.vrstaDokumenta == "Moji zadaci") {
var dokumenti =
await _dokumentiService!.getForApprovalByType(widget.tipId ?? 0);
}
if (widget.vrstaDokumenta == "Moji dokumenti") {
var dokumenti =
await _dokumentiService!.getMojiDokumentiByType(widget.tipId ?? 0);
}
var dokumenti =
await _dokumentiService!.getSharedDocumentsByType(widget.tipId ?? 0);
setState(() {
_dokumenti = dokumenti;
_dokumentiSearch = dokumenti;
isLoading = false;
});
});
I'm always getting this code executed:
var dokumenti =
await _dokumentiService!.getSharedDocumentsByType(widget.tipId ?? 0);
Appreciate if someone can advise. Thank you in advance!
CodePudding user response:
write it like this and see what happens:
if (widget.vrstaDokumenta == "Moji zadaci") {
var dokumenti =
await _dokumentiService!.getForApprovalByType(widget.tipId ?? 0);
}
else if (widget.vrstaDokumenta == "Moji dokumenti") {
var dokumenti =
await _dokumentiService!.getMojiDokumentiByType(widget.tipId ?? 0);
}else{
var dokumenti =
await _dokumentiService!.getSharedDocumentsByType(widget.tipId ?? 0);
}
CodePudding user response:
Basically what happens is that your setting the value of dokumenti always twice, first you set the right value but then you overwrite it by calling _dokumentiService.getSharedDocumentsByType(). Consider wrapping this in a condition as well.
However using switch/case would be suited best for what you are trying to do.
switch(widget.vrstaDokumenta) {
case "Moji zadaci":
var dokumenti =
await _dokumentiService!.getForApprovalByType(widget.tipId ?? 0);
case: "Moji Dokumenti":
var dokumenti =
await _dokumentiService!.getMojiDokumentiByType(widget.tipId ?? 0);
default:
var dokumenti =
await _dokumentiService!.getSharedDocumentsByType(widget.tipId ?? 0);
}
Consider wrapping the switch case statement inside a seperate function to make it even cleaner.