Deploying Flutter Web Applications to Platforms Like Firebase Hosting or GitHub Pages

Flutter, Google’s UI toolkit, allows developers to build natively compiled applications for mobile, web, and desktop from a single codebase. One of the appealing features of Flutter is its ability to create web applications that can be deployed to various platforms such as Firebase Hosting and GitHub Pages. In this comprehensive guide, we’ll explore how to deploy a Flutter web application to these popular platforms.

Prerequisites

  • A Flutter project.
  • Flutter SDK installed on your machine.
  • Firebase account (for Firebase Hosting).
  • GitHub account (for GitHub Pages).
  • Node.js and npm installed (recommended for GitHub Pages).

Step 1: Building Your Flutter Web Application

Before deploying, you need to build your Flutter web application. Open your terminal, navigate to your Flutter project directory, and run the following command:

flutter build web

This command compiles your Flutter code into HTML, CSS, and JavaScript files and places them in the /build/web directory of your project.

Step 2: Deploying to Firebase Hosting

Firebase Hosting is a fast, secure, and reliable hosting service provided by Google. It’s an excellent choice for hosting single-page web apps, static content, and dynamic content via Cloud Functions.

Setting up Firebase CLI

If you haven’t already, install the Firebase CLI. Open your terminal and run:

npm install -g firebase-tools

After the installation is complete, authenticate with Firebase using your Google account:

firebase login

Initializing Firebase in Your Flutter Project

Navigate to your Flutter project’s root directory in the terminal and run:

firebase init

Follow the prompts:

  • Select “Hosting: Configure and deploy Firebase Hosting sites”.
  • Choose your Firebase project (or create a new one).
  • Set your public directory to build/web.
  • Configure as a single-page app (SPA): yes.
  • Set up automatic builds and deploys with GitHub: no (unless you want CI/CD).

Deploying Your App

Deploy your Flutter web application to Firebase Hosting with the command:

firebase deploy

This command uploads the contents of your build/web directory to Firebase Hosting. Once the deployment is complete, Firebase will provide you with a hosting URL where your application is accessible.

Step 3: Deploying to GitHub Pages

GitHub Pages allows you to host a static website directly from your GitHub repository. It’s a simple and free way to showcase your Flutter web application.

Configuring Your GitHub Repository

  • Ensure your Flutter project is a GitHub repository.
  • Create a new repository or push your existing project to GitHub.

Creating a gh-pages Branch (Option 1)

GitHub Pages can serve your website from a specific branch named gh-pages. Here’s how to create and deploy using this method:


# Navigate to the web build directory
cd build/web

# Initialize a new Git repository
git init

# Add all files
git add .

# Commit the files
git commit -m "Initial commit"

# Create a new branch named gh-pages
git branch -M gh-pages

# Add your remote GitHub repository
git remote add origin [your-repository-url]

# Push the gh-pages branch to GitHub
git push -u origin gh-pages

Go to your GitHub repository settings, navigate to the “Pages” section, and set the source to the gh-pages branch. Your website will be live at https://[your-username].github.io/[your-repository-name].

Using GitHub Actions (Option 2)

GitHub Actions can automate the deployment process. This approach is more streamlined and efficient for ongoing updates.


# Create a file at .github/workflows/deploy.yml

name: Deploy Flutter Web to GitHub Pages

on:
  push:
    branches:
      - main # or your default branch

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.x' # specify Flutter version
          channel: 'stable'
      - run: flutter config --enable-web
      - run: flutter build web --release

      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./build/web
  • Create a file named deploy.yml inside the .github/workflows/ directory in your repository.
  • Paste the above YAML configuration into the file.
  • Customize the flutter-version according to your project.
  • The action automatically builds and deploys your Flutter web app whenever you push changes to the main branch.

After setting up GitHub Actions, any push to your specified branch (e.g., main) will trigger the deployment. Go to your repository settings, navigate to the “Pages” section, and set the source to the gh-pages branch (if it’s created by the action) or the /docs folder on your main branch.

Custom Domain (GitHub Pages)

You can configure GitHub Pages to use a custom domain. Add a CNAME file to your web directory with your custom domain:

echo "yourdomain.com" > build/web/CNAME

Then, configure your domain’s DNS settings to point to GitHub Pages.

Best Practices

  • Optimize Your Flutter Web App: Use code splitting, asset optimization, and tree shaking to reduce the bundle size.
  • Test Thoroughly: Test your web application on different browsers and devices to ensure compatibility and performance.
  • Monitor Performance: Use tools like Google Analytics or Firebase Performance Monitoring to track your app’s performance and identify areas for improvement.
  • Secure Your App: Use HTTPS and other security best practices to protect your users’ data.

Troubleshooting

  • Firebase Deploy Fails: Check your Firebase project settings and ensure you have the necessary permissions.
  • GitHub Pages Not Updating: Verify that your GitHub Actions workflow is configured correctly and running without errors.
  • App Not Loading Correctly: Clear your browser cache or try a different browser.

Conclusion

Deploying Flutter web applications to platforms like Firebase Hosting or GitHub Pages is a straightforward process that enables you to share your creations with the world. Whether you choose Firebase for its comprehensive hosting capabilities or GitHub Pages for its simplicity, Flutter’s versatility makes web deployment accessible to all developers. Follow this guide to deploy your Flutter web apps efficiently and effectively, and ensure your audience can experience the best of what Flutter has to offer.