Underscore for unused parameters

Last updated: 12.09.2020 - 16:00 by Boehrsi

Sometimes you have to deal with given functions, interface implementations or builders, with parameters you don’t use. When utilizing those you can use an underscore to actively mark unneeded parameters as not used in your code. You can use multiple underscores if multiple parameters are unused.

DO replace unused parameters with underscores

return MaterialApp(
    localeResolutionCallback: (locale, _) { // Good
        Intl.defaultLocale = locale.toString();
        return locale;
    },
);

return MaterialApp(
    localeResolutionCallback: (_, __) { // Good
        return Locale('en', '');
    },
);

DON'T name unused paramters

return MaterialApp(
    localeResolutionCallback: (locale, supportedLocales) { // Bad, as supportedLocales is never used
        Intl.defaultLocale = locale.toString();
        return locale;
    },
);

return MaterialApp(
    localeResolutionCallback: (locale, supportedLocales) { // Bad, as locale and supportedLocales are never used
        return Locale('en', '');
    },
);