نمط BLoC مع Streams
تعلّم نمط BLoC (Business Logic Component) في Flutter لفصل منطق الأعمال عن واجهة المستخدم باستخدام الأحداث والحالات وStreams.
نمط BLoC مع Streams درس مجاني في Flutter Mobile Development على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flutter Mobile Development، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flutter Mobile Development 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What Is BLoC?
BLoC stands for Business Logic Component. It is a pattern that keeps your business logic out of widgets by turning events into states through a stream.
- UI sends events in
- BLoC processes them
- New states flow back out to the UI
Why Another Pattern?
You already know setState, Provider, and Riverpod. BLoC shines when logic is complex and you want:
- Clear separation of UI and logic
- Predictable, testable state transitions
- A reactive stream-based flow
Streams Refresher
A Stream is a sequence of asynchronous values over time. A StreamController lets you push values and expose them as a stream.
final controller = StreamController<int>();
controller.stream.listen((value) => print(value));
controller.add(1);
controller.add(2);Events
An event is an immutable class describing something the user did. Define them as plain Dart classes.
abstract class CounterEvent {}
class Increment extends CounterEvent {}
class Decrement extends CounterEvent {}States
A state is an immutable snapshot of what the UI should show. Keep it simple and serializable.
class CounterState {
final int count;
const CounterState(this.count);
}The Bloc Class
Using the flutter_bloc package, a Bloc maps events to states with on<Event> handlers.
class CounterBloc extends Bloc<CounterEvent, CounterState> {
CounterBloc() : super(const CounterState(0)) {
on<Increment>((e, emit) => emit(CounterState(state.count + 1)));
on<Decrement>((e, emit) => emit(CounterState(state.count - 1)));
}
}Providing the Bloc
Wrap part of the tree in BlocProvider so descendants can access the bloc.
BlocProvider(
create: (_) => CounterBloc(),
child: const CounterPage(),
);Reacting with BlocBuilder
BlocBuilder rebuilds widgets whenever a new state is emitted.
BlocBuilder<CounterBloc, CounterState>(
builder: (context, state) => Text('Count: ' + state.count.toString()),
);Dispatching Events
Send events to the bloc with context.read<CounterBloc>().add(...).
FloatingActionButton(
onPressed: () => context.read<CounterBloc>().add(Increment()),
child: const Icon(Icons.add),
);BlocListener for Side Effects
Use BlocListener for one-time side effects like showing a SnackBar or navigating, where you do not want to rebuild UI.
BlocListener<CounterBloc, CounterState>(
listener: (context, state) {
if (state.count == 10) ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Reached 10!')),
);
},
child: child,
);Cubit: A Lighter BLoC
A Cubit drops events and exposes methods directly that call emit. Use it when full event classes feel like overkill.
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
void increment() => emit(state + 1);
}Quick Check
Which widget should you use to trigger a one-time SnackBar without rebuilding UI on every state?
Recap
You learned the BLoC pattern:
- Events in, states out, via streams
- Bloc with on<Event> handlers, or a lighter Cubit
- BlocProvider, BlocBuilder and BlocListener
BLoC gives you clean, testable separation of logic and UI for complex apps.
الأسئلة الشائعة
هل درس «نمط BLoC مع Streams» مجاني؟
نعم — نص درس «نمط BLoC مع Streams» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flutter Mobile Development، انتقل إلى CoddyKit PRO. تتضمن دورة Flutter Mobile Development 4 دروس في المجموع.
ماذا ستتعلم في «نمط BLoC مع Streams»؟
تعلّم نمط BLoC (Business Logic Component) في Flutter لفصل منطق الأعمال عن واجهة المستخدم باستخدام الأحداث والحالات وStreams. تتمرن على Flutter Mobile Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flutter Mobile Development؟
لا تُشترط خبرة سابقة. Flutter Mobile Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «نمط BLoC مع Streams»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flutter Mobile Development هذا؟
نعم. كل درس في Flutter Mobile Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- setState وInheritedWidget
- أساسيات حزمة Provider
- Riverpod لإدارة الحالة
- نمط BLoC مع Streams