<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Rana Ayman</title>
    <description>The latest articles on Forem by Rana Ayman (@ranaayman).</description>
    <link>https://forem.com/ranaayman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F390902%2Fd0fbbeaf-ad68-44d0-83c8-da84d82aefa2.png</url>
      <title>Forem: Rana Ayman</title>
      <link>https://forem.com/ranaayman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ranaayman"/>
    <language>en</language>
    <item>
      <title>bloc</title>
      <dc:creator>Rana Ayman</dc:creator>
      <pubDate>Fri, 19 Jul 2024 08:27:36 +0000</pubDate>
      <link>https://forem.com/ranaayman/bloc-4bfc</link>
      <guid>https://forem.com/ranaayman/bloc-4bfc</guid>
      <description>&lt;p&gt;&lt;strong&gt;Efficient State Management in Flutter with Cubit and Freezed:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the realm of Flutter development, managing state efficiently and effectively is crucial for building responsive and maintainable applications. One powerful combination for state management is using Cubit from the BLoC (Business Logic Component) package alongside the Freezed package. This approach not only provides a clear separation of concerns but also enhances code readability and maintainability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up Cubit and Freezed&lt;/strong&gt;&lt;br&gt;
Let's walk through an example where we implement a login feature using Cubit and Freezed. The directory structure is organized to ensure each screen has its own logic folder containing the necessary Cubit files. For the login feature, we'll create a login_cubit.dart and login_state.dart.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Directory Structure&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lib/
│
├── features/
│   ├── login/
│   │   ├── logic/
│   │   │   ├── cubit/
│   │   │   │   ├── login_cubit.dart
│   │   │   │   └── login_state.dart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Defining the State with Freezed&lt;/strong&gt;&lt;br&gt;
In login_state.dart, we define the different states for the login process using the Freezed package. Freezed simplifies the creation of union types (a.k.a. sealed classes), making our state definitions clean and expressive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First, add the necessary dependencies in pubspec.yaml:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dependencies:
  flutter_bloc: ^8.1.3
  freezed: ^1.0.0
  freezed_annotation: ^1.0.0
  flutter:
    sdk: flutter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Then, create the login_state.dart file:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9wai3gn3665k8x1p28bk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9wai3gn3665k8x1p28bk.png" alt="Image description" width="800" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this file:&lt;/p&gt;

&lt;p&gt;initial represents the initial state when the Cubit is created.&lt;br&gt;
loading is used to indicate that the login process is ongoing.&lt;br&gt;
success holds the data returned upon a successful login.&lt;br&gt;
error carries an error message if the login process fails.&lt;br&gt;
to generate the necessary code for the Freezed package, we use the part directive and run a code generation command. The part 'login_state.freezed.dart'; line tells the Dart compiler to include the generated file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generating the Code&lt;/strong&gt;&lt;br&gt;
To generate the code, run the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dart run build_runner build --delete-conflicting-outputs 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Implementing the Cubit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next, we'll implement the login_cubit.dart to manage the state transitions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjboyw3xqf0itvyc09fxc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjboyw3xqf0itvyc09fxc.png" alt="Image description" width="800" height="997"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the LoginCubit:&lt;/p&gt;

&lt;p&gt;We initiate the Cubit with the initial state.&lt;br&gt;
The login method simulates a network call, emitting a loading state during the process.&lt;br&gt;
If the login is successful, we emit a success state with some dummy data.&lt;br&gt;
In case of failure, we emit an error state with an appropriate error message.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Utilizing the Cubit in UI&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, we integrate the Cubit into our UI to handle state changes and display corresponding views.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbshs812fzgur3e86rj1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbshs812fzgur3e86rj1.png" alt="Image description" width="800" height="1116"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;LoginBlocListener widget that listens to state changes in a LoginCubit and reacts to different states by displaying dialogs for loading, success, and error conditions.&lt;/p&gt;

&lt;p&gt;In the LoginScreen:&lt;/p&gt;

&lt;p&gt;LoginScreen integrates the LoginBlocListener to handle state changes during the login process.&lt;br&gt;
The LoginBlocListener in the login screen is responsible for monitoring the state changes of the LoginCubit and performing appropriate actions based on the current state. It helps manage the user interface's response to different login states (e.g., loading, success, error) without cluttering the main LoginScreen widget code.&lt;/p&gt;

</description>
      <category>cubit</category>
      <category>flutter</category>
      <category>dart</category>
    </item>
    <item>
      <title>Code Review</title>
      <dc:creator>Rana Ayman</dc:creator>
      <pubDate>Fri, 22 May 2020 12:16:49 +0000</pubDate>
      <link>https://forem.com/ranaayman/code-review-2ihh</link>
      <guid>https://forem.com/ranaayman/code-review-2ihh</guid>
      <description>&lt;p&gt;Questions to Ask Yourself When Conducting a Code Review:&lt;/p&gt;

&lt;p&gt;Is the code clean and modular?&lt;br&gt;
Can I understand the code easily?&lt;br&gt;
Does it use meaningful names and whitespace?&lt;br&gt;
Is there duplicated code?&lt;br&gt;
Can you provide another layer of abstraction?&lt;br&gt;
Is each function and module necessary?&lt;br&gt;
Is each function or module too long?&lt;/p&gt;

&lt;p&gt;Is the code efficient?&lt;br&gt;
Are there loops or other steps we can vectorize?&lt;br&gt;
Can we use better data structures to optimize any steps?&lt;br&gt;
Can we shorten the number of calculations needed for any steps?&lt;br&gt;
Can we use generators or multiprocessing to optimize any steps?&lt;/p&gt;

&lt;p&gt;Is documentation effective?&lt;br&gt;
Are in-line comments concise and meaningful?&lt;br&gt;
Is there complex code that's missing documentation?&lt;br&gt;
Do function use effective docstrings?&lt;br&gt;
Is the necessary project documentation provided?&lt;/p&gt;

&lt;p&gt;Is the code well tested?&lt;br&gt;
Does the code high test coverage?&lt;br&gt;
Do tests check for interesting cases?&lt;br&gt;
Are the tests readable?&lt;br&gt;
Can the tests be made more efficient?&lt;/p&gt;

&lt;p&gt;Is the logging effective?&lt;br&gt;
Are log messages clear, concise, and professional?&lt;br&gt;
Do they include all relevant and useful information?&lt;br&gt;
Do they use the appropriate logging level?&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
    </item>
  </channel>
</rss>
