1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] How to explicit pass 'undefined' to a function, trying to fall back to default value?

Discussão em 'Mobile' iniciado por Stack, Outubro 5, 2024 às 10:32.

  1. Stack

    Stack Membro Participativo

    Say I have a function named printInt.

    void printInt({int v = 0}) {
    print(v);
    }


    I read integer v from some JSON map object.

    void main(){
    ...
    int v = someJSONMap['v']; // i don't know whether v is null

    // i want to do something like this:
    // (which not valid in dart)
    // printInt(v: v == null ? undefined : v);
    }


    I can not change printInt (not belongs to me), and I don't want to write codes like this cause the real situation is much more complex.

    // i don't want to write like this:
    if(v == null){
    print();
    }else{
    print(v);
    }


    Is there any alternative solution for the above?

    Continue reading...

Compartilhe esta Página