Navigation in Flutter: `pushReplacement` vs `pushAndRemoveUntil`

pushReplacement

Imagine you're replacing an old road sign with a new one. The old one is gone, and there's no way to know it was ever there. That's what pushReplacement does in Flutter.

Usage:

  • Navigator.pushReplacement(context, newRoute)

What It Does:

  • Replaces the current route with a new route.
  • The current route is removed from the stack, making way for the new route.
  • The removed route is completely gone—pressing the back button won’t bring it back.

When to Use It:

  • Ideal for scenarios like replacing a login screen with a home screen after a user logs in. You don’t want them to go back to the login screen, right?

Code Example:

Navigator.pushReplacement(
  context,
  MaterialPageRoute(builder: (context) => HomeScreen()),
);

pushAndRemoveUntil

Now, think of pushAndRemoveUntil as clearing a cluttered desk, keeping only the essentials. You decide which items stay and which go. This function allows more control over which routes to remove.

Usage:

  • Navigator.pushAndRemoveUntil(context, newRoute, (route) => condition)

What It Does:

  • Pushes a new route and removes routes below it until a specified condition is met.
  • You provide a condition to determine which routes to keep or discard.

When to Use It:

  • Perfect for situations like logging out, where you want to return to a clean slate (the login screen) and remove all previous screens.

Code Example:

Navigator.pushAndRemoveUntil(
  context,
  MaterialPageRoute(builder: (context) => LoginScreen()),
  (Route<dynamic> route) => false, // Remove all previous routes.
);

Key Differences

  1. Scope of Operation:

    • pushReplacement swaps only the top route.
    • pushAndRemoveUntil can clear out multiple routes based on your condition.
  2. Stack Control:

    • pushReplacement provides straightforward, one-route replacement.
    • pushAndRemoveUntil allows for a more flexible navigation reset.
  3. Use Case Scenarios:

    • Use pushReplacement for simple replacement needs.
    • Use pushAndRemoveUntil for complex navigation flows requiring cleanup of multiple routes.

A sample nginx config file that redirects traffic from a domain to b domain

# This config is for old-domain.com => new-domain.com redirecting
server {
  listen 80;
  listen 443 ssl;
  server_name old-domain.com www.old-domain.com;
  location / {
    return 301 https://new-domain.com$request_uri;
  }
  #include /etc/nginx/conf.d/snippets/ssl.conf;
}


# A sample nginx config file for a reverse proxy server
server {
    server_name happynotes-img-uploader.shukebeta.com;
    client_max_body_size 10M;

    access_log  /var/log/nginx/happynotes-img-uploader.shukebeta.com.access.log;
    error_log   /var/log/nginx/happynotes-img-uploader.shukebeta.com.error.log;

    location / {
        proxy_set_header X-Forwarded-Proto https;
        proxy_pass http://127.0.0.1:3000;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    large_client_header_buffers 4 32k;

    listen [::]:80;
    listen 80;
}

gh pr view can't find PR just created

TLDR;

The solution is git push -u

David.Wei @ /d/git/xemt-core/dfx - [feature/mt-38546-axo] $ git push -u
branch 'feature/mt-38546-axo' set up to track 'origin/feature/mt-38546-axo'.
Everything up-to-date
David.Wei @ /d/git/xemt-core/dfx - [feature/mt-38546-axo] $ gh pr view
MT-38546 Fintrac API| DFX DB changes #1355
Open • David-Wei_euronet wants to merge 6 commits into integration from feature/mt-38546-axo • about 3 days ago
...

vilmibm wrote the following on this issue and it reminds me why I couldn't find the PR:

gh pr view relies on mapping the current locally checked out branch to a remote tracking branch so that the github host can be queried appropriately for PR data; in your example it seems like you're creating PRs without any configured tracking information, making it impossible for gh to open the PR.

To avoid this issue happening in the future, you can run the following command to enable the auto tracking feature.

git config --global push.autoSetupRemote true

If your git version is lower and couldn't upgrade easily, another measure is to create an alias like the following

git config --global alias.p 'push -u origin HEAD'

Then always use git p instead of git push will do the trick.

Two practical ORACLE procedures for adding/dropping fields

DECLARE
    -- Procedure to add a column if it does not exist
    PROCEDURE add_column_if_not_exists (
        p_owner       VARCHAR2,
        p_table_name  VARCHAR2,
        p_column_name VARCHAR2,
        p_column_type VARCHAR2
    ) IS
        l_sql    VARCHAR2(1024);
        l_count  NUMBER;
    BEGIN
        -- Check if column exists
        SELECT COUNT(*)
        INTO l_count
        FROM ALL_TAB_COLUMNS
        WHERE OWNER = p_owner
          AND TABLE_NAME = p_table_name
          AND COLUMN_NAME = p_column_name;

        -- Add column if it does not exist
        IF l_count = 0 THEN
            l_sql := 'ALTER TABLE ' || p_owner || '.' || p_table_name || ' ADD (' || p_column_name || ' ' || p_column_type || ')';
            DBMS_OUTPUT.PUT_LINE(l_sql);
            EXECUTE IMMEDIATE l_sql;
        END IF;
    END;

    -- Procedure to drop a column if it does exist
    PROCEDURE drop_column_if_exists (
        p_owner       VARCHAR2,
        p_table_name  VARCHAR2,
        p_column_name VARCHAR2
    ) IS
        l_sql    VARCHAR2(1024);
        l_count  NUMBER;
    BEGIN
        -- Check if column exists
        SELECT COUNT(*)
        INTO l_count
        FROM ALL_TAB_COLUMNS
        WHERE OWNER = p_owner
          AND TABLE_NAME = p_table_name
          AND COLUMN_NAME = p_column_name;

        -- drop column if it does exist
        IF l_count = 1 THEN
            l_sql := 'ALTER TABLE ' || p_owner || '.' || p_table_name || ' DROP COLUMN ' || p_column_name;
            DBMS_OUTPUT.PUT_LINE(l_sql);
            EXECUTE IMMEDIATE l_sql;
        END IF;
    END;
BEGIN
...
END;
/

A tip to ease the process creating new feature branch

We use feature/ticket-number-team-abbr as feature branch name, such as feature/mt-1234-axo at work.

If you mainly work with a huge repository. Everytime, you switch to the integration/main branch and run git pull before you create a feature branch, it would take a while as it needs to update your work directory and normally there are a lot of files changed.

I use a different way which is a little bit speedy. Whatever which branch you are now in, run

git fetch && git checkout -b feature/mt-xxxx-axo origin/integration 

It helps but I still need to type the above, which is still a lot of letters! So, I create a bash script to help me more,

see the following command nbi 38546

nbi 38546
remote: Enumerating objects: 22, done.
remote: Counting objects: 100% (22/22), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 22 (delta 17), reused 22 (delta 17), pack-reused 0
Unpacking objects: 100% (22/22), 4.43 KiB | 5.00 KiB/s, done.
From github.com:Euronet-XE/DFX
   941796a8a..11f76bff7  feature/mt-38183-fugu -> origin/feature/mt-38183-fugu
 * [new branch]          feature/mt-39492-ice  -> origin/feature/mt-39492-ice
 * [new branch]          feature/mt-39744-ice-card -> origin/feature/mt-39744-ice-card
branch 'feature/mt-38546-axo' set up to track 'origin/integration'.
David.Wei @ /d/git/xemt-core/dfx - [feature/mt-38546-axo] $

what’s in this nbi script? here it is

cat ~/bin/nbi
#!/bin/bash

# Check if the current directory is a git repository
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
    echo "Error: Current directory is not a git repository."
    exit 1
fi

# Run git fetch to get the latest changes from the remote repository
git fetch

# Determine the action based on the script name
case "$(basename "$0")" in
    nbi)
        # Check if an argument is provided
        if [[ $# -eq 1 ]]; then
            # Run git checkout to create and switch to a new branch based on the integration branch
            git checkout -b "feature/mt-$1-axo" "origin/integration"
            exit $?
        else
            echo "Usage: $0 <anumber>"
            exit 1
        fi
        ;;
    nbm)
        # Check if an argument is provided
        if [[ $# -eq 1 ]]; then
            # Run git checkout to create and switch to a new branch based on the main branch
            git checkout -b "feature/mt-$1-axo" "origin/main"
            exit $?
        else
            echo "Usage: $0 <anumber>"
            exit 1
        fi
        ;;
    *)
        echo "Error: Unsupported script name."
        exit 1
        ;;
esac

why do I call it nbi ? haha, it is a ABBR from new branch from integration

Wait, why did I mention nbm in the script? Yes, you can copy this nbi script with name nbm , then using the nbm command it will work the same way with the repositories that use main as the integration branch.

Did you see? How lazy I am!

2024-07-22 Update: I also wrote another script sw to ease the switching branches operation:

#!/bin/bash

# Check if the user provided a number
if [ -z "$1" ]; then
  echo "Usage: sw <number>"
  exit 1
fi

# Construct the branch name
branch_name="feature/mt-$1-axo"

# Switch to the branch
git switch "$branch_name"

# Check if the switch was successful
if [ $? -eq 0 ]; then
  echo "Switched to branch '$branch_name'"
else
  echo "Failed to switch to branch '$branch_name'"
fi