Skip to content

Run the Flutter web app on a specific port

Published: at 12:36 PM

Running a Flutter Web App on a Specific Port

Flutter is a powerful framework that allows developers to build beautiful, natively compiled applications for mobile, web, and desktop from a single codebase. When working with Flutter web apps, there might be times when you need to run your application on a specific port, whether for testing, development, or deployment purposes. This blog post will guide you through the steps required to run a Flutter web app on a specific port.

Prerequisites

Before we dive into the steps, make sure you have the following installed:

If you haven’t set up Flutter yet, you can follow the official Flutter installation guide to get started.

Steps to Run Flutter Web App on a Specific Port

Ensure Web Support is Enabled

First, ensure that your Flutter environment is set up to support web development. You can enable web support by running the following commands in your terminal:

flutter channel stable
flutter upgrade
flutter config --enable-web

Create or Open a Flutter Project

You can create a new Flutter project or open an existing one. To create a new project, run:

flutter create my_flutter_web_app
cd my_flutter_web_app

Run Flutter Web App on a Specific Port

To run your Flutter web app on a specific port, use the flutter run command with the --web-port flag followed by the port number you want to use. For example, to run your app on port 8080, you would execute:

flutter run -d chrome --web-port=8080

In this command:

Access the Flutter Web App

Once the app is running, open your web browser and navigate to http://localhost:8080 (or the port number you specified). You should see your Flutter web app running on the specified port.

Stopping the Flutter Web App

To stop the running app, return to your terminal and press Ctrl + C.

Additional Tips

Specifying a Host

If you need to run your Flutter web app on a specific host (e.g., to allow access from other devices on the same network), you can use the --web-hostname flag. For example:

flutter run -d chrome --web-port=8080 --web-hostname=0.0.0.0

This will make your app accessible from other devices using your machine’s IP address and the specified port.

Debugging

If you encounter issues while running your Flutter web app, check the terminal output for error messages. Flutter provides detailed logs that can help you diagnose and fix problems.

Hot Reload and Restart

Flutter’s hot reload and hot restart features are available for web apps as well. Use r in the terminal for hot reload and R for hot restart while the app is running.

Running your Flutter web app on a specific port can be crucial for various development and testing scenarios. By following the steps outlined in this guide, you can easily configure your Flutter environment to meet your needs.

Configuring VSCode to Run Flutter Web App on a Specific Port

If you’re using Visual Studio Code (VSCode) for Flutter development, you can configure it to run your Flutter web app on a specific port by modifying the .vscode/launch.json file. This file allows you to set up custom configurations for running and debugging your app. Here’s how you can set it up:

Open Your Flutter Project in VSCode

Open your Flutter project in VSCode. If you don’t have a .vscode folder in your project directory, you’ll need to create it.

Create or Modify launch.json

Inside the .vscode folder, create a file named launch.json if it doesn’t already exist. Add the following configuration to this file:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Flutter Web",
      "type": "dart",
      "request": "launch",
      "program": "lib/main.dart",
      "args": ["-d", "chrome", "--web-port=8080"]
    }
  ]
}

In this configuration:

Running the Configuration

After saving the launch.json file, you can run your Flutter web app using this configuration:

  1. Open the Run and Debug view by clicking the play icon in the sidebar or by pressing Ctrl + Shift + D.
  2. In the top dropdown menu, select the “Flutter Web” configuration (or whatever name you gave it).
  3. Click the green play button to start the debugging session.

VSCode will run your Flutter web app on the specified port (8080 in this case), and you can access it by navigating to http://localhost:8080 in your web browser.

Additional Tips

Conclusion

By setting up the launch.json file in your .vscode directory, you can easily configure VSCode to run your Flutter web app on a specific port. This setup is particularly useful for streamlining your development process and ensuring consistency across different development environments.