Codenic Clean Architecture
  • Introduction
  • The Clean Architecture
    • Presentation Layer
    • Domain Layer
    • Infrastructure Layer
  • Tutorial
    • Overview
    • Creating a Flutter Modular Project
      • Tips for Managing a Modular Project
    • Implementing the Domain Layer
      • Core Dependencies
      • Creating an Entity
      • Creating a Failure
      • Creating a Repository Interface
      • Creating Use Cases
        • CRUD Operations (Runner)
        • Data Streams (Watcher)
    • Implementing the Infrastructure Layer
      • External Dependencies
      • Creating a Data Model
      • Creating a Data Source
      • Implementing a Repository
    • Implementing the Presentation Layer
      • External Dependencies
      • Dependency Injection and Service Locator
      • Widgets
        • Snackbar Handler
        • Global Blocs Widget
        • Note Widgets
  • Packages
    • Codenic Bloc Use Case
      • Runner
      • Watcher
    • Codenic Logger
      • Usage
      • Example
      • Modifying the Logger
      • Integrating Firebase Crashlytics to the logger
    • Codenic Exception Converter
      • Failure Object
      • Exception Converter
      • Exception Converter Suite
      • Example
Powered by GitBook
On this page
  1. Tutorial
  2. Implementing the Domain Layer
  3. Creating Use Cases

Data Streams (Watcher)

PreviousCRUD Operations (Runner)NextImplementing the Infrastructure Layer

Last updated 2 years ago

A is a use case for streaming data that continuously emits a Left Event (error) or Right Event (data) whenever our stream emits an error or new data, respectively.

The Left Event, in our case, is one of the we previously created, whereas the Right Event is a list of on our app. Every time a new note is created, updated or deleted, our Watcher will emit a new Right Event with an updated list of the most recent notes we have.

To create a Watcher use case for streaming a list of our most recent note entries, under the note_app/modules/domain/lib/note/use_cases/ directory, create a file called watch_note_entries.dart:

import 'dart:async';

import 'package:codenic_bloc_use_case/codenic_bloc_use_case.dart';
import 'package:codenic_exception_converter/codenic_exception_converter.dart';
import 'package:domain/note/entities/note_entry.dart';
import 'package:domain/note/repositories/note_repository.dart';

/// {@template WatchNoteEntriesParams}
/// The parameters for the [WatchNoteEntries] use case.
/// {@endtemplate}
class WatchNoteEntriesParams with EquatableMixin {
  /// {@macro WatchNoteEntriesParams}
  const WatchNoteEntriesParams({required this.limit});

  /// The maximum number of [NoteEntry]s to stream.
  final int limit;

  @override
  List<Object?> get props => [limit];
}

/// {@template WatchNoteEntries}
/// See [NoteRepository.watchNoteEntries].
/// {@endtemplate}
class WatchNoteEntries
    extends Watcher<WatchNoteEntriesParams, Failure, List<NoteEntry>> {
  /// {@macro WatchNoteEntries}
  WatchNoteEntries({required this.noteRepository});

  /// A reference to the [NoteRepository] instance.
  final NoteRepository noteRepository;

  @override
  FutureOr<Either<Failure, VerboseStream<Failure, List<NoteEntry>>>> onCall(
    WatchNoteEntriesParams params,
  ) =>
      noteRepository.watchNoteEntries(limit: params.limit);
}
Watcher
Failure objects
Note entities