I have a website that I'm already hosting at www.example.com
(custom domain) using Firebase Hosting. I already have various HTML pages on the site like example.com/page-1.html
or example.com/page-2.html
.
Now I need to build a web application example-web
and put it in a page within the already existing site. Imagine example.com/page-3
should load the Flutter Web application only. I do not want my existing HTML pages to change or be affected by the Flutter single page web application.
Some things to note:
example.com
. These mobile apps are also created with Flutter.example.com/profile/username
needs to load the flutter web app after reading the variable username
.Any help or guidance for this will be highly appreciated! Thank you!
Flutter works with existing code, is used by developers and organizations around the world, and is free and open source. In this lab, you will create a Firebase Meetup application. The application will demonstrate how to use Firebase Web authentication in a Flutter application.
Web apps are supported on the stable channel in Flutter 2.0. In this post, we’ll take a look at deploying a web app to Firebase static hosting and front it with a custom domain. We’ll deploy an existing application to firebase hosting.
With a single command, you can quickly deploy web apps and serve both static and dynamic content to a global CDN (content delivery network). You can also pair Firebase Hosting with Cloud Functions or Cloud Run to build and host microservices on Firebase. The first requirement is to have a Google Account.
Build the app for deployment using the flutter build web command. You can also choose which renderer to use by using the --web-renderer option (See Web renderers ). This generates the app, including the assets, and places the files into the /build/web directory of the project. The release build of a simple app has the following structure:
The default for a flutter web app build is build/web . This is what the interaction would look like: ? Please select an option: Use an existing project ? Select a default Firebase project for this directory: shiped-7b848 (shipantherproject) i Using project shiped-7b848 (shipantherproject)? What do you want to use as your public directory? build/web
You can simply paste all the files that Flutter
generates in your Firebase hosting's public directory. Follow these steps:
1. Run Flutter Build:
flutter build web
It will generate build/web
directory and it's content may look something like:
2. Copy build files in public directory: If you already have a Firebase hosting project, then I assume your public directory looks something like:
After copying all files from the Flutter build directory to the public directory, it should look like:
Do note that I've renamed index.html
from Flutter to page-3.html
and kept the original index.html
as is.
That page-3.html
file will look for main.dart.js
file in the same directory so if you move that JS file to any other directory, let's say, /js/main.dart.js
then don't forget to change that in page-3.html
.
3. Serve the website:
Try serving your website locally using firebase serve --only hosting
command.
You should have all existing pages as they were and Flutter app on http://localhost:3000/page-3.html
. If the page returns 404 on /page-3
but works on /page-3.html
then checkout this answer on cleanUrls.
PS: Make sure none of your files overwrite each other if they have same name.
When you run flutter build web
in your project directory Flutter automatically creates an index.html
file along with all of its dependencies, thus serving a Flutter web app is not that different to serving a normal page. All the files you need will end up in your project's /build/web
folder, which can be served on its own at example.com/page-3
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With