1
I have the following function, which returns me either "FREE" or "PREMIUM". The return of the database works perfectly. Here is the code.
userInfoConst() async {
try {
var uid = await AuthenticationProvider().getUserId();
ApiProvider apiProvider = ApiProvider();
final usuarioModel =
await apiProvider.getRequest('usuarios/$uid');
UsuarioModel model = UsuarioModel.fromJson(usuarioModel.data);
var plano = model.plano;
print(plano);
return plano;
}
What I intend to do is make the following parole:
if (ApplicationConsts().userInfoConst() == 'FREE') {
_banner
..load()
..show(anchorType: AnchorType.bottom);
}
The problem is that it never enters the code within the conditional, even though the condition is met.
After much debug and research I discovered the following: - It is not possible to make an "asynchronous conditional", that is, you cannot use async/await together in if. - The value that is returned from my function does not arrive in time to make the comparison with the string 'FREE', so it never enters.
I have tried using Futurebuilder, but it did not work. I have tried to put my function inside initState but also without success. I still believe you have the futureBuilder wrong...
How can I solve this problem?
I don’t know where you are making this comparison but you have already tried to "solve" the asynchronous method before making the comparison?
var info = await ApplicationConsts().userInfoConst()
would take the result, after you are with it comparesif(info == "FREE") { ... }
.– Leonardo Paim
@Leonardopaim I make the comparison within the
widget build
. There’s no way to await it directly as you mentioned. I would have to use it in a method with async. I’ve tried that too, but without success. So in a way I tried that when you said– augusto francisco
I even imagined that you were doing inside the widget anyway, as you did not give the executable code to test I quoted something as superficial as possible... could you provide a functional example of your code? One that can be taped to Dartpad just to make it easier for everyone.
– Leonardo Paim
@Leonardopaim the code is on the company computer, so it will not be available now...
– augusto francisco