1

I am trying to create a nested enum in flutter/dart. it will serve as a structure to use as keys in certain maps holding their values. something like this :

enum Settings{
  WIFI_SETTINGS,
  UI_SETTINGS,
  STORAGE_SETTING {
    FTP_SETTINGS {
      HOST,
      PORT,
      KEY
    },
    DB_SETTING {
      TYPE,
      HOST,
      PORT,
      UNAME,
      PWD
    }
  }
}

then to be able to use it like this

Map AllSettings = Map<Settings,dynamic>();

print(AllSettings[Settings.WIFI_SETTINGS]);
print(AllSettings[Settings.STORAGE_SETTING.FTP_SETTINGS.PORT]);

Clearly the above code is not acceptable within dart, is there anyway of achieving this ?

Kaki Master Of Time
  • 1,428
  • 1
  • 21
  • 39
  • You can draw inspiration from that. https://stackoverflow.com/questions/980766/how-do-i-declare-a-nested-enum – lsaudon Jun 15 '20 at 21:39
  • Would `AllSettings[Settings.STORAGE_SETTING.FTP_SETTINGS.PORT]` really be much better than `AllSettings[Settings.STORAGE_SETTING_FTP_SETTINGS_PORT]`? – jamesdlin Jun 15 '20 at 23:55
  • 1
    Trying your code in an editor seems to start giving issues when you embed { inside the enum since it is expecting } to end the enum. Nested classses, string keys (i.e. `AllSettings["storage.ftp.port"]`), or nested Maps (`AllSettings[Settings.STORAGE][StorageSettings.FTP][FTPSettings.PORT]` might also work – MoralCode Jan 30 '22 at 17:14

0 Answers0