
بطّل توصف الشاشة بـ flags: الـ sealed classes والـ exhaustive switch في Dart 3
- كاتب موثّق
- Dart 3
محتويات المقال
build بتاع شاشة الـ profile في أول frame خالص. الـ Cubit بدأ بـ const ProfileState(): مش بيحمّل، ومفيش error، ومفيش user لسه. والـ build method شيّك على الـ loading، وبعدين على الـ error، وبعدين عمل اللي الفرع الأخير دايمًا بيعمله: state.user!.name. ومحدش كتب فرع لحالة "لسه مفيش حاجة حصلت"، لأن محدش فكّر فيها كحالة أصلًا.
الفكرة الغلط الشائعة
isLoading وerror وdata هي أبسط طريقة آمنة توصف بيها شاشة. class واحدة وcopyWith واحد، وأي widget تقدر تقرا أي field.
اللي بيحصل فعلًا
تلات fields، تمن حالات
bool واتنين nullable fields بيدّوك 2 × 2 × 2 = 8 تركيبات ممكنة. والشاشة ليها معنى مع تلاتة منهم تقريبًا: بيحمّل، اتحمّل ومعاه user، فشل ومعاه رسالة. والباقي لسه Dart سليم. ProfileState(isLoading: true, error: 'Timeout', user: mona) بيعدّي من الـ compile، ولما بعتناه للـ build method اللي تحت، الشاشة عرضت spinner ورمت الـ error والـ user الاتنين من غير أي صوت.
كل if وعد الـ compiler مايقدرش يتأكد منه
if هو اللي بيحدد الشاشة هتعرض إيه، والفرع الأخير بيفترض إن كل اللي قبله استبعد الحالات الوحشة. وهنا بييجي الـ !: معناه "ثق فيّا، الـ user موجود". شغّل الـ build method دي على الـ ProfileState() الابتدائي، وهترمي نفس الـ crash اللي في الـ report بالظبط: TypeError بـ "Null check operator used on a null value".
الـ sealed بيقفل العيلة
abstract كلهم "require a language version of at least 3.0". واللي انت محتاجه هنا هو sealed: "To create a known, enumerable set of subtypes, use the sealed modifier." والـ محدش يقدر يعمل لها extend أو implement برّه الـ library بتاعتها، وهي "implicitly abstract"، فمش هتقدر تعمل ProfileState عادي. ولأن كل الـ subtypes المباشرة عايشة في نفس الـ library، فـ "the compiler is aware of any possible direct subtypes".
الـ switch لازم يغطي كل حالة
ProfileLoaded(:final user) بيعمل bind للـ user جوه الـ case ده بس. والـ docs بتاعة الـ patterns بتقولها كده: "The values that a pattern destructures in a case become local variables." يعني مفيش nullable field، ومفيش !.
نعيد كتابة الـ profile state
class ProfileState { const ProfileState({this.isLoading = false, this.error, this.user}); final bool isLoading; final String? error; final User? user;}Widget buildProfile(ProfileState s) { if (s.isLoading) return const CircularProgressIndicator(); if (s.error != null) return Text(s.error!); return Text(s.user!.name); // nothing stops user from being null here}sealed class ProfileState { const ProfileState();}final class ProfileLoading extends ProfileState { const ProfileLoading();}final class ProfileLoaded extends ProfileState { const ProfileLoaded(this.user); final User user;}final class ProfileFailed extends ProfileState { const ProfileFailed(this.message); final String message;}Widget buildProfile(ProfileState s) => switch (s) { ProfileLoading() => const CircularProgressIndicator(), ProfileLoaded(:final user) => Text(user.name), ProfileFailed(:final message) => Text(message), };ProfileLoading()، لأن الشاشة اللي مستنية داتا هي شاشة بتحمّل. وعشان نشوف الـ compiler وهو بيشتغل، ضفنا final class ProfileOffline extends ProfileState وشغّلنا dart analyze. طلّع error، مش warning: "The type 'ProfileState' isn't exhaustively matched by the switch cases since it doesn't match the pattern 'ProfileOffline()'." يعني كل switch محتاج يتعدّل بقى في ليستة قدّامك، قبل ما التطبيق يشتغل.
emit بيقارنها، فاللي شايلة داتا منها محتاجة value equality، من == أو من Equatable. والمثال اللي في الـ FAQ بتاع Bloc نفسه بيعمل كده بالظبط: sealed class MyState extends Equatable، وكل final class بتحط الـ fields بتاعتها في الـ props.
! | ||
if القديمة مش بتاخد بالها | ||
تطبقها إزاي في مشروعك
اكتب الحالات اللي المستخدم بيشوفها اكتبها لشاشة واحدة: بتحمّل، اتحمّلت، فاضية، فشلت، offline. ولو حالتين مستحيل يظهروا على الشاشة مع بعض، يبقوا حالتين منفصلين. ادّي كل حالة الداتا بتاعتها بس final classلكل حالة تحت base عليهاsealed. الحالة الـ loaded فيها user مش nullable، والحالة الـ failed فيها رسالة.اعمل switch في الـ build من غير wildcard استخدم switch expression فيه case لكل subtype، وطلّع اللي محتاجه بالـ destructuring، فالـ build method مايبقاش فيها !.ابدأ من حالة حقيقية اختار initial state الـ switch بيغطيها أصلًا، زي ProfileLoading()، بدل object فاضي.خلّي الـ compiler يقود التغيير لما تضيف حالة، شغّل الـ analyzer وصلّح كل error بيطلّعه. الليستة دي هي الـ change set بتاعك.
الخلاصة
كل boolean أو nullable field بيضاعف عدد الحالات اللي الـ class تقدر تشيلها، سواء عايزها أو لأ. الـ flags class بتخلّي الحالات "المستحيلة" مسموحة، والـ !بيحوّلها لـ crashes وقت التشغيل.الـ sealedclass في Dart 3 بتثبّت مجموعة الـ subtypes، فالـ compiler بيعرف كل حالة.الـ switch على sealed type لازم يبقى exhaustive، والحالة الناقصة compile error. ابعد عن _في الـ switch على الـ sealed states بتاعتك، وسيب الـ flags model للحالات اللي بتتداخل فعلًا.
التعليقات (0)