Showing posts with label Flutter. Show all posts
Showing posts with label Flutter. Show all posts

Friday, 24 October 2025

⚡️ Debugging the Delay: Flutter & Firebase Latency Traps & How to Escape Them ๐Ÿš€

1. The Problem Statement: The Navigation Wait ๐Ÿฅถ

Scenario: A user taps "Confirm Payment." Your backend successfully processes the transaction and saves the data to Firestore in milliseconds.

Observed Bug: A noticeable lag of 0.5 to 1.5 seconds occurs after the payment is confirmed but before the "Payment Success!" screen appears. The user feels the app is slow or stuck.

The Goal: The confirmation screen must appear instantly to reward the user and maintain a premium app feel.


2. Root Causes: The Double-Blockade ๐Ÿšง

The lag is caused by two fundamental mistakes in how asynchronous tasks are handled, both of which unnecessarily block the main UI thread.

A. Blockade 1: The Serial await Trap (Unnecessary Waiting)

  • The Problem: Your code uses await on every single background task, even if the navigation doesn't depend on it.

  • Example of Bad Code:

    Dart
    // After payment is successful:
    final receipt = await service.generateReceipt(); // Essential, must await
    await analytics.trackOrderConversion(); // ❌ Don't need to await this!
    await user.updateTotalOrders();        // ❌ Don't need to await this!
    // Navigation can only happen after ALL three are complete.
    
  • The Effect: You serialise tasks that could run concurrently, forcing the user to wait for non-essential background processes to finish.

B. Blockade 2: The Synchronous notifyListeners() Block (The Final Freeze)

  • The Problem: You call notifyListeners() (or a similar state change) right before navigating.

  • The Effect: The notifyListeners() call executes synchronously on the UI thread, forcing a complete rebuild of every widget listening to that state (e.g., the complex "Account Dashboard" or "Order History" screen). This massive rebuild must finish before the return statement can run, leading to the final, observable freeze just before navigation.


3. Design Best Practices: Decouple All Non-Essential Work ๐Ÿš€

To eliminate all lag, you must apply two principles: 

1. Never block for non-essential data and 

2. Defer heavy UI work.

Step 1: Remove Unnecessary Blocking

Allow background tasks to run concurrently without await.

Dart
// Inside your ViewModel (after the primary payment write completes)

// Essential task (must await)
final receipt = await service.generateReceipt();

// Non-essential tasks (run in the background without blocking)
analytics.trackOrderConversion(); 
user.updateTotalOrders();

Step 2: Decouple the UI Rebuild with Future.microtask

This is the critical fix to solve the final lag caused by the synchronous rebuild.

Action in the ViewModelRationaleCode Principle
Local UpdateUpdate local variables instantly (fast).totalOrders += 1;
The CRITICAL FixDefer the heavy rebuild. Pushes the expensive synchronous rebuild to the next available event cycle.Future.microtask(() => notifyListeners());
ReturnRelease the UI thread. The function returns, allowing navigation to execute immediately.return receipt;

Final Code Strategy (Putting it all together):

Dart
// Inside your ViewModel.confirmPayment

final receipt = await service.generateReceipt(); // Await essential task
    
// 1. Synchronously update local state
totalOrders += 1;
orders.insert(0, receipt);

// 2. ๐ŸŒŸ CRITICAL FIX: Defer heavy rebuild ๐ŸŒŸ
Future.microtask(() => notifyListeners()); 

// 3. Trigger background refreshes (no await)
analytics.trackOrderConversion(); 
user.updateTotalOrders();

// 4. Return immediately (Enables instant navigation)
return receipt; 

By applying these two fixes, you ensure the user is instantly celebrated on the success screen, and all data updates and rebuilds are handled efficiently in the background.

Thursday, 23 October 2025

๐Ÿš€ The Phantom Zero: Fixing Flutter/Firestore Inconsistent Counts After a Write ๐Ÿ›’

1. The Problem Statement ๐Ÿ›

This is one of the most common and frustrating bugs when you start working with Flutter and Cloud Firestore.

The Scenario: You build an e-commerce feature. A user adds the very first item to their cart, triggering a successful database write.

The Problem: Your app's UI immediately displays contradictory information:

  • The detailed list widget correctly shows the item, and the sub-count for the category (e.g., 'Electronics') displays 1.

  • The critical "Total Cart Items" badge in the header shows 0.

This "Phantom Zero" bug persists briefly until the app is refreshed or a short time passes, then the total count corrects itself to 1. Why does the total count lie, even though the database confirmed the write?


2. The Root Cause: Eventual Consistency ⏳

The cause is not a simple coding error, but a fundamental characteristic of high-scale databases like Firestore: Eventual Consistency.

The Mechanism of Failure:

  1. The Write: Your Flutter app successfully writes the new cart item document to the primary Firestore server.

  2. Replication Delay: To handle millions of users, Firestore must copy this new data across all its global read servers (replicas). This process is extremely fast, but it is not instantaneous.

  3. The Race: Immediately after the write, your app's ViewModel executes two independent read queries:

    • Call A (Total Count): The query for the simple total (snapshot.size) often hits a read server that has not yet received the new data. Result: 0 (Stale Data).

    • Call B (Detailed Count): The query for the detailed item map (getItemsByCategory()) hits a slightly different, or fresher, server that has the new data. Result: {'Electronics': 1 (Correct Data)}.

The simultaneous access to inconsistent servers is the database race condition that causes the total count to lag behind the category count.


3. The Solution: Enforce Consistency in the ViewModel ๐Ÿ›ก️

The reliable, production-ready solution is to avoid relying on two potentially inconsistent database reads. We must enforce consistency within the Flutter ViewModel (or Provider/BLoC) by deriving the total count from the more reliable detailed data.

๐Ÿ› ️ The Fix in Dart (ViewModel)

  1. Eliminate the Unreliable Read: Stop calling the simple repository.getTotalCartItems() database query.

  2. Single Source of Truth: Rely only on the detailed map query.

  3. Local Derivation: Calculate the total count by summing the values in the detailed map using Dart's fold method.

Dart
// Inside your CartViewModel (or BLoC)

Future<void> fetchCartSummary() async {
    final userId = FirebaseAuth.instance.currentUser?.uid;
    if (userId == null) return;

    try {
        // 1. Fetch the detailed, reliable map from Firestore
        // (This result contains the newly added item: {'Electronics': 1})
        categoryCounts = await repository.getItemsByCategory(userId);
        
        // 2. CRITICAL: Derive the Total Count locally for guaranteed consistency
        // totalItems is now guaranteed to be the sum of all category counts.
        totalItems = categoryCounts.values.fold(0, (sum, count) => sum + count); 

        notifyListeners(); // Update the Flutter UI
    } catch (e) {
        // Handle error
    }
}

By making the total count logically dependent on the detailed category map, you bypass the brief moment of database inconsistency, ensuring your Flutter app delivers the expected "jet speed" consistency to the user.

Tuesday, 5 August 2025

Firebase Phone Auth Not Working on Emulator ??

 


๐Ÿงฉ Problem Statement:

You're trying to implement Phone Number Authentication in Flutter with Firebase :

  • ✅ In Firebase Console test phone number and verification code set correctly 

  • OTP screen flashes for a split second in the Mobile App

  • ❌ Then you’re redirected to Chrome browser, without being able to enter the OTP



  • ❌ Or app seems to "hang" or fail silently

This can mislead developers into thinking the Firebase setup is broken, when in fact — it's an emulator problem.



๐ŸŽฏ Root Cause:

The problem arises when your emulator doesn't have Google Play Services.

Firebase Phone Auth uses Google Play Services to manage:

  • reCAPTCHA flows

  • SafetyNet checks

  • SMS auto-retrieval and manual entry screen

Without it, Android redirects to a browser to complete verification — which often fails silently in the emulator.


✅ Verified Fix (Working Emulator Setup):

1. Delete Any Emulator Without Google Play

Go to Android Studio > Device Manager
๐Ÿ—‘️ Delete emulators that don’t show “Google Play” in the “Play Store” column.

2. Create a New Emulator (Pixel 6a Recommended)

  • Click “Create Virtual Device”

  • Choose a modern Pixel (Pixel 6a works great)

  • Proceed to System Image screen

3. Select the Correct System Image

✅ Choose:

API 33 | Google Play | x86_64

Look for these clues:

  • ✅ “Google Play” tag in green

  • ✅ “Play Store” column shows a checkmark after creation

  • ✅ Emulator shows Play Store icon on home screen

4. Check SDK Components (One-Time)

Open Android Studio > SDK Manager > SDK Tools tab.
Ensure the following are installed:

  • ☑️ Google Play Services

  • ☑️ Android SDK Platform for API 33

  • ☑️ Intel x86 Emulator Accelerator

  • ☑️ Android Emulator

Then restart Android Studio.




๐Ÿงช Test the Flow Again

  1. Launch your new emulator

  2. Run your Flutter app

  3. Initiate Phone Auth

  4. ✅ You’ll now see the OTP entry screen stay and function properly — no Chrome redirection!


๐Ÿ› ️ Summary

If your Firebase Phone Auth:

  • ✅ Sends OTP

  • ❌ Flashes the OTP screen

  • ❌ Redirects to Chrome

๐Ÿ‘‰ You are likely using an emulator without Google Play Services.

Fix it by switching to a Google Play system image (e.g., Pixel 6a, API 33 with Google Play).