Does angular dart have a concept of overloaded methods?

Issue

I want to have 3 different functions all called "AddField" where each function has different parameters. Based on the parameters, the language will know which function to call. Delphi has this concept if you mark the function as "overload".

  void addField(int tableID, fieldID) {
    addfield(tableID, fieldID, 0, 0)
  }

  void addField(int tableID, fieldID, groupID) {
    addfield(tableID, fieldID, groupID, 0)
  }

  void addField(int tableID, fieldID, groupID, size) {
    item = FieldItem();
    item.tableID = tableID;
    item.fieldID = fieldID;
    item.groupID = groupID;
    item.size = size;
    fieldlist.add(item);
  }

and they can be used this way…

  void addClientFields(){
    addfield(tblIDClient, fldIDName, grpContact, 30);
    addfield(tblIDClient, fldIDActive);
    addfield(tblIDClient, fldIDDate, grpSettings);
  }

Solution

Dart doesn’t have Method overloading. You can make method with different name or use optional paramater with default value.

void addField(int tableID, fieldID, [int groupID = 0, int size = 0]) {
    item = FieldItem();
    item.tableID = tableID;
    item.fieldID = fieldID;
    item.groupID = groupID;
    item.size = size;
    fieldlist.add(item);
  }

Answered By – Petrus Nguyễn Thái Học

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *