Is there a way to implement inner classes in dart?

Viewed 1536
//access to a Constant if it was like another language 
ImageWeather.Desktop.cloud; 
 
//What i wanted ( not working because there is no inner class)
class ImageWeather {
      class Desktop {
         static const String cloud = 'assets/images/DesktopCloud.png';
         static const String noCloud = 'assets/images/DesktopNoCloud.png';
         static const String rain = 'assets/images/DesktopRain.png';
   }  
      class Phone{
         static const String cloud = 'assets/images/PhoneCloud.png';
         static const String noCloud = 'assets/images/PhoneNoCloud.png';
         static const String rain = 'assets/images/PhoneRain.png';
   }
}

//access to a String like an inner class with new code
ImageWeather.desktop.cloud;


class ImageWeather {
   static final Desktop desktop = Desktop();
   static final Phone phone = Phone();
}

class Desktop {
   get cloud => 'assets/images/DesktopCloud.png';
   get noCloud => 'assets/images/DesktopNoCloud.png';
   get rain => 'assets/images/DesktopRain.png';
}
class Phone {
   get cloud => 'assets/images/PhoneCloud.png';
   get noCloud => 'assets/images/PhoneNoCloud.png';
   get rain => 'assets/images/PhoneRain.png';

}

I'm currently learning Dart and I just see that I can't use an inner class. Is there a way to write something similar to inner classes in Dart?

1 Answers

Whether or not using Inner Classes is a good or bad thing is subjective. Different developers will have different opinions.

However, if you wish to make Desktop and Phone only visible to ImageWeather, you can write the three classes within a single package and rename the 'inner' classes _Desktop and _Phone, thus making them only visible within that package.

Related