How to deploy flutter web on server? - dart

I was learning Flutter web. Now I want to deploy this code in the real server.
The flutter code here: in the lib folder
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter layout demo',
home: Scaffold(
appBar: AppBar(
title: Text('Flutter layout demo'),
),
body: Center(
child: Text('Hello World'),
),
),
);
}
}
How can I deploy this code on the server ? I am new on the Flutter web.

[UPDATE]
To create a production build for web, you can now directly run flutter build web command similar to other platforms (android and ios)
and you will see build/web folder generated with the assets folder and you can simply deploy it on your server.
[OLD ANSWER STEP 1 & 2 No longer required ]
you need to do a production build by using a webdev tool,
To install webdev you need a pub tool.
so go to the location where you have dart SDK installed and inside the bin folder you should have a pub batch file. You need to provide the bin folder's path to the environment variable in order to use pub from cmd.
open cmd/terminal run the below command to install webdev
pub global activate webdev
now go to the root folder of your project and do a build in release mode
flutter build web
you should see a build folder (/build/web) in the root directory, just copy that folder and host it on a web server.
I used the same way to deploy it to GitHub pages here's how in detail guide.
Some useful link: https://dart.dev/tools/webdev#build
Here's the running flutterweb app

You can deploy your flutter web app in a shared hosting with Nodejs or in VPS server with Python Follow this Medium Blog Post
After you build your flutter web app with "flutter build web" and you want to host it in a shared hosting plan prepare your nodejs app as a simple server for your flutter web app here is sample code
app.js
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public-flutter')));
module.exports = app;
package.json
{
"name": "flutter-web-app",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"morgan": "~1.9.1"
}
}
Create a folder and name it (“public-flutter”) and then put your flutter web app in the folder you have just created so nodejs can serve it through his server if you are in a shared hosting just continue with the Blog Post here
and if you are in a VPS server then run this command if you want to server the nodejs app
node app.js
or if you don't want nodejs just use python in your flutter web app to serve it as a simple http server with this command
nohup python -m SimpleHTTPServer 8000 &
Just make sure you are in your web app folder when you run the command. “nohub” will let the command keep running even if you closed the SSH session on Linux .
Or you can server your app through Dart pub/webdev tools by using the dhttpd package.

if you want to use your own server over web e.q your virtual private host or other host over net :
go to the root folder of your project and do a build in release mode flutter build web,then
upload (/build/web)directory to your server ,
you can follow this link and configure IIS on windows server.

Here is the simple way to deploy your flutter web application on amazon web server.
Following is the simple process i follow.
Build flutter web: flutter build web —release
Create instance on aws ec2 server: mean allocate some memory for
your website on server. Instance is the virtual server in aws cloud.
Connect to your server(instance) with the help of putty :
Install Vesta control panel on your server. (you can install other control panel
too if you don't like vesta).
Upload your content(website) on server.(With the help of
FileZilla you can easily upload
your website content on server)
Here is the simple video tutorial:
https://youtu.be/htuHNO9JeRU

Nota Bene - Using flutter 2.0 as of today, I am seeing a bug where the images are not uploaded. Performance is also not the best as of
today. Bookmark this and I will update with workarounds.
One solution for those who use VPS/Shared hosting of any kind:
Open the local build folder
Compress (zip) the web folder
Upload the web.zip file (ftp or file manager)
On your server (for example on cPanel choose Extract from the File
Manager)
Rename the web folder to whatever you like
Note that if you are not using the website's root directory you must rename the
folder in index.html from "/" to "/web/" (to match the name above, see eg.)
If you are using the website root folder make sure you move all the files
from inside of /web into your / website's root folder (not your
server)
eg.
<base href="/"> to <base href="/web/">
Update for missing images issue
tldr; Two issues - In pubspec.yaml I forgot to uncomment the assets folder and when built for web, the images were placed one
folder below the assets folder. Editing the pubspec.yaml and moving
the images folder up one level (after building for the web) solved the
issue.
My flutter project folder has an assets/images folder in the project root. After not seeing the image online (it was visible during debugging), I found that running flutter build web --release caused my assets folder to be recreated within build/web/assets/assets/images. My code correctly referenced assets/images/file.png. I just moved the images folder up one level when deploying to the web. So my web server root now has assets/images/name.png and the image is showing fine.
Solution - The fix for this was to use the proper structure for assets which is to create top level folders for (images and
google_fonts) in the flutter project root. Then the compiler builds
the web project correctly. My code references image assets as
images/name.png. Pubspec.yaml should reference the assets: as
- images/ and - google_fonts/

If it's a Firebase project you can use Firebase Hosting.
It will ask you to install Firebase Tools on your system and you will have to initialize it on the root folder of your project.
Then you just have to:
flutter build web
firebase deploy
and refresh browser (maybe with ctrl + F5 or ctrl + shift + r)

Related

How to seperate React application from Ruby on Rails created with webpack configuration?

I found an open source Ruby on Rails project that is created with command rails new myapp --webpack=react but I am only interested in the React part. How can I seperate it from the rest and enable it to run on its own?
The link to repo: https://github.com/vigetlabs/storyboard
For what I have done so far;
React related files reside in directory ./app/javascript/pack/ but npm script triggers Ruby application first. Ruby, somehow, starts the React application. This is how far I could come.
use create-react-app to create a new react app.
copy and paste /app/javascript/src to the newly created react app. maybe packs folder too.
copy over the package.json file content to get all packages.
do some testing/debugging

How to solve error "Firefox can’t find the file at moz-extension://......" when running a built blank Quasar BEX ("quasar dev -m bex" works)

These are the commands I ran (I didn't change the code in any way):
quasar upgrade -i
mkdir folder
cd folder
quasar create
quasar mode add bex
npm i
quasar build -m bex
cd /dist/bex/UnPackaged && web-ext sign --api-key ... --api-secret ...
I then drag&dropped that signed .xpi file (in the folder ./web-ext-artifacts/) into the latest Firefox ESR in Debian 10 stable (78.12.0esr) and installed the addon (with no other addons being installed/running in that Firefox profile). When I click onto the Quasar (v2.0.1) icon in the upper right of Firefox after it finished installing it shows this error page:
File not found Firefox can’t find the file at moz-extension://extension-id/www/index.html.
instead of the proper Vue/Quasar page. The same also occurs with the unsigned addon (after setting xpinstall.signatures.required to enable installation) but it does work fine when running quasar dev -m bex.
Why is that and how to solve it?
It appears to be due to problems with the filepaths when using the build-command rather than the dev command. One can change the url in address bar to show the page but neither could I change all places where 'index.html' or process.env.DEV is used to make it work, nor would that be the build-process as is which I assume ought to work (as is; as said I only ran those few commands for a completely blank BEX and didn't change the code for testing).
Seems like this is bug since quasar: 2.0.0-beta.1 moved to stable:
This is not a good long term solution, but I did the following as a temporary fix:
install execa
npm i execa
in quasar.conf.js
const execa = require('execa')
...
build: {
async afterBuild(cfg) {
try {
// bit of a hack to move the index.html into the www folder on build of Packaged
const dirUnPackaged = cfg.quasarConf.bex.builder.directories.input
const { stdout } = await execa('mv', [`${dirUnPackaged}/index.html`, `${dirUnPackaged}/www/`])
console.log(stdout)
} catch (error) {
console.error(error.message)
}
},
}
Essentially moving the index.html file into the www folder in the Package

Assets folder resolution: Running an application within a project not picking up assets

I created an application within an Angular workspace. When running
ng serve [application-name]
Picks up images and files in asset folder fine. Now I want to run the workspace with just
ng serve
I would expect through having the application lazy loaded the path would resolve but instead I get
zone.js:3243 GET http://localhost:4200/assets/terms.txt 404 (Not Found)
What is the proper setup to access assets of an application within a workspace?
EDIT After talking offline I think we've found a solution. I'll post it here and leave the below for reference even though the original answer I posted doesn't really apply to the problem.
You're trying to run multiple Angular apps and route between them via something like Firebase where you can direct different routes to different apps.
To get this to work locally for development you will need to run each Angular app separately on it's own port. I suggest you control the destination of the route in the environment files. This way when you are running locally you can point to the port the app is running on and then point to the endpoint Firebase uses in production.
Example environment.ts file in your root app
{
...,
OTHER_APP_URL: 'localhost:4201'
}
prod.environment.ts
{
...,
OTHER_APP_URL: '/otherApp'
}
Then in your component
....
import {environment} from '<path to environments file>';
#Component({
selector: 'my-component',
template: '<a [href]="otherAppUrl"></a>'
})
export class MyComponent implements OnInit {
otherAppUrl: string;
ngOnInit() {
this.otherAppUrl = environment.OTHER_APP_URL;
}
}
You will probably need to do something similar in the other apps so you can route from them to the root app or other child apps. You will probably also need to build the other apps with the --baseHref flag when you build for production so their assets are available. See here for more info from the docs: https://angular.io/cli/build
Old answer - doesn't really apply to the question
Looking at your repo I don't see the terms.txt in your root project's assets folder. I checked to see if it was in one of the other libraries in the repo but wasn't able to find it there either.
If this is an asset that is included or referenced by a component or service in one of your libraries you will need to copy that over to the library's output folder as part of your build process since that functionality isn't currently supported by the Angular CLI.
An example of a build script that might do this for you is:
ng build my-lib-with-assets --prod && cp -r projects/my-lib-with-assets/src/assets dist/my-lib-with-assets && ng build --prod
Don't forget that you need to build your libraries before you build your main project.

Mimicking build_runner serve on a nodeJs server

With the expiration of Dartium that happened just a few days ago, I felt compelled to migrate from dart 1.24.3 to Dart2, even though it is still in dev.
I have although hit a few walls doing so, one of them being related to the architecture of my apps.
I run a nodeJs server, which also acts as a webserver with client side dart.
The problem that I experience with the new dart SDK is that in order for the .dart files to be read in Chrome, they must be served using webdev serve or build_runner serve.
Obviously, these 2 commands act as the file server, which is not what I want since I'm using a nodeJS server.
By using build_runner watch I think I am enabling the build and watch of the .dart files into .dart.js inside of the following directory :
.dart_tool/build/generated//lib
I am also able to serve them from my nodeJS server. What remains is the package directory, I can't seem to find where pub serves gets the following package files:
/packages/$sdk/dev_compiler/amd/require.js
/packages/$sdk/dev_compiler/amd/dart_sdk.js
Does anyone know what build_runner serve does to include them?
Thank you,
There are 2 options for using a different server during development.
Run build_runner serve on a different port and proxy the requests to it from your other server. This has the benefit of delaying requests while a build is ongoing so you don't get an inconsistent set of assets.
Run build_runner watch --output web:build and use the created build/ directory to serve files from. This will include a build/packages directory that has these files in it.
These files are served from the lib directory of the dart sdk itself.
Note that there is another option, which is to use the -o option from build_runner. This will create a merged directory with source and generated files, which you can serve directly without relying on any internal file layout.

Dart app with bin, web and libs

I want to make Dart app that has flexible deployment. It can be started as a web server or standalone app in browser as well. My directory structure:
bin
- httpserver.dart
lib
- commonlib.dart
web
- web.html
- web.dart
pubspec.yaml
I wanto start either httpserver.dart providing web's content or web.html directly in Chromium. I have troubles with the lib visibility from bin/httpserver.dart. using the "import 'package:prj/commonlib.dart'" does not work. But from the web.dart is works fine.
Please advice how to share libs among bin's and web's code. Or I should I make structure of dirs somehow different?
Note: there is no packages sub-dir in the bin directory created by pub get. I am using dart sdk 1.7.2.
Thank you, Ladislav.
In the bin directory there should be a packages symlink created automatically but it is not in subdirectories of prj/bin. If the symlink isn't created just create it manually.

Resources