flutterdart

How to make different names of parameters and variables in functions of Dart


I'm trying to make the parameter names in the function differed from variables. Something like this

///metacode for example
void addValue(Value value, {required Context in context}) {
// where:
//  `Context` it is a type,
//  `in` it is visible name,
//  and last `context` it is a variable  
}
// then for calling the function we can do...
final Value myValue = ...;
final Context myContext = ...;
addValue(myValue, in: myContext);

So, can we have the difference between the visible names of the parameters and variables? Or not? And if it is possible how I can do it? Because I don't found any solution for it.


Solution

  • Dart does not have Swift and Objective-C's notion of having separate external and internal parameter names. You sometimes can just use the external names you want and create internal aliases:

    void function({required ArgumentType externalName})
      var internalName = externalName;
    }
    

    However, some limitations compared to Swift and Objective-C are:

    But also note that for Swift and Objective-C, those "external names" are essentially part of the function name, which is why they don't need to be unique and which is why you can't reorder them. The Swift and Objective-C designers did that so that function calls could read like sentences. Dart's named parameters are order-independent, so you can't force callers to use your desired, sentence-friendly order.