What's the meaning of passing underscore _ “_” (_) when calling a function in dart/flutter?

前端 未结 1 1101
醉酒成梦
醉酒成梦 2021-02-13 19:53

When reading dart code I often see some functions called with just an underscore _ parameter. It\'s bugging me out for some time now and since flutter has improved its analysis

1条回答
  •  盖世英雄少女心
    2021-02-13 20:43

    Underscore is normally an indication that you are not going to use that parameter inside the block it is just a good way to write code, for instance:

    method(int useful, int useless) {
      // say I am only going to use 'useful' in this block 
    }
    

    Above code can also be written as:

    method(int useful, int _) {
      // using '_' means I'm not going to use 2nd parameter in the block
    }
    

    Answer to your question now:

    builder: (_, counter, __) => Translations(counter.value),
    

    means you have 3 parameters _, counter and __, and only counter is what you are using, so 1st and 3rd parameters are denoted with _ and __. This is just cleaner way to write code.

    0 讨论(0)
提交回复
热议问题