Protected keyword in Dart

Nijat Namazzade
1 min readOct 31, 2022

--

⚡ Today, I want to talk about the use case of the @𝒑𝒓𝒐𝒕𝒆𝒄𝒕𝒆𝒅 keyword in the Dart programming language. However, Dart does not support the protected visibility in the same way some of you could expect it to come from the other OOP language’s background such as C# or Java — we can only annotate these properties as protected.

⚡The protected keyword is an access modifier used for attributes, methods, and constructors, making them accessible in the same package and subclasses.

The use case of protected keyword

⚠️ But it is more as a reminder for the developer not to use them outside the class scope (𝑉𝑖𝑠𝑢𝑎𝑙 𝑆𝑡𝑢𝑑𝑖𝑜 𝐶𝑜𝑑𝑒 𝑒𝑑𝑖𝑡𝑜𝑟 𝑒𝑣𝑒𝑛 𝑠ℎ𝑜𝑤𝑠 𝑎 𝑤𝑎𝑟𝑛𝑖𝑛𝑔 𝑖𝑛 𝑡ℎ𝑖𝑠 𝑐𝑎𝑠𝑒). @𝒑𝒓𝒐𝒕𝒆𝒄𝒕𝒆𝒅 produces a warning if public members are referenced by code from other libraries that is not within subclasses.

🍒 @𝒑𝒓𝒐𝒕𝒆𝒄𝒕𝒆𝒅 is an annotation (and not a language keyword) provided by 𝑝𝑎𝑐𝑘𝑎𝑔𝑒:𝑚𝑒𝑡𝑎 and that is used by 𝑑𝑎𝑟𝑡𝑎𝑛𝑎𝑙𝑦𝑧𝑒𝑟

--

--

Nijat Namazzade

I love applying responsive design principles and watching the things I created shrink into mobile screens and still look amazing.