How to open compiled Flutter web index.html in browser locally?
Asked Answered
T

3

8

Running flutter build web creates index.html file in /build/web/ along with

assets                    icons                     manifest.json
favicon.png               index.html                version.json
flutter_service_worker.js main.dart.js

The current flutter channel is beta.

$ flutter channel

Flutter channels:  
  master  
  dev  
* beta  
  stable

flutter --version

Flutter 1.25.0-8.1.pre • channel beta • https://github.com/flutter/flutter.git
Framework • revision 8f89f6505b (2 weeks ago) • 2020-12-15 15:07:52 -0800
Engine • revision 92ae191c17
Tools • Dart 2.12.0 (build 2.12.0-133.2.beta)

When I try to open /build/web/index.html in the Chrome browser, it shows blank. Is it usual or do I need to install any node packages to make it run?

Note: I followed this doc https://flutter.dev/docs/get-started/web.

Does it have anything to do with this code in index.html which does not seem to be included in the earlier flutter versions?

  <!--
    If you are serving your web app in a path other than the root, change the
    href value below to reflect the base path you are serving from.

    The path provided below has to start and end with a slash "/" in order for
    it to work correctly.

    Fore more details:
    * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
  -->
  <base href="/">
Terryterrye answered 2/1, 2021 at 5:12 Comment(0)
P
16

you need to install node.js server from npm to run the website on.

npm install -g http-server

Change directory to /build/web and run the server on localhost with:

npx http-server
Psychopathist answered 12/1, 2021 at 1:59 Comment(4)
Awesome! Thanks.Terryterrye
@SawThinkarNayHtoo np :)Psychopathist
Hey I hav a issue actually I started server on local host and then I am unable to exit my project from there know i am panicking I am afraid because of hackers and stealers I hav no idea in this area I unfortunately closed the terminal so I am unable to control + c and I don't know how many host are running and how please help me. AsapCosette
@Lucian closing the terminal stops the http server i guess. You can try solutions suggested in #14791410Psychopathist
G
6

Adding to the topic, you can also use python to server it: Set up a local testing server

\[flutter_project]\build\web> python -m http.server

or

\[flutter_project]\build\web> python -m SimpleHTTPServer

Then go to localhost:8000

Glengarry answered 5/7, 2021 at 20:35 Comment(1)
I had to use python3. \[flutter_project]\build\web> python3 -m http.serverRevalue
L
2

For me this worked:

  1. Download Python from python.org and install it.
  2. Open command prompt and type your build/web path like this: cd C:\Users\YourUSER\Documents\build\web
  3. Type python -m http.server 8080 -> This is another port exclusively for your app.
  4. Open Edge Browser and type: http://localhost:8080
  5. Click on the icon button on top right (install yourApp to run on your own page) And thats it! You can create a shortcut on your desktop to run automatically!
Lilialiliaceous answered 29/7, 2023 at 3:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.