<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MichaelHo</title>
    <description>The latest articles on DEV Community by MichaelHo (@michelo851a1203).</description>
    <link>https://dev.to/michelo851a1203</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F327466%2F323396c6-05dc-427d-8a7e-424360436234.jpeg</url>
      <title>DEV Community: MichaelHo</title>
      <link>https://dev.to/michelo851a1203</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/michelo851a1203"/>
    <language>en</language>
    <item>
      <title>Build and test a simple flutter web app using webdev and live-server</title>
      <dc:creator>MichaelHo</dc:creator>
      <pubDate>Sat, 15 Jun 2024 07:11:20 +0000</pubDate>
      <link>https://dev.to/michelo851a1203/build-and-test-a-simple-flutter-web-app-using-webdev-and-live-server-20hl</link>
      <guid>https://dev.to/michelo851a1203/build-and-test-a-simple-flutter-web-app-using-webdev-and-live-server-20hl</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;As a frontend web developer specializing in vue.js. This is my first try to build flutter web app .Let's begin!&lt;/p&gt;

&lt;h2&gt;
  
  
  first try
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step1: Create a flutter app
&lt;/h3&gt;

&lt;p&gt;To start, create a new Flutter project by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;flutter create test_demo_app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: &lt;strong&gt;I have tried &lt;code&gt;flutter create testDemoApp&lt;/code&gt; or &lt;code&gt;flutter create test-demo-app&lt;/code&gt; Flutter CLI does not allow these naming conventions.Therefore I use &lt;code&gt;snake case&lt;/code&gt; to start my project.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now We can use following command to run and demonstrate my web app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;flutter run &lt;span class="nt"&gt;-d&lt;/span&gt; web-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As You can see it serve on &lt;a href="http://localhost:50367"&gt;http://localhost:50367&lt;/a&gt;. &lt;br&gt;
open this URL in your preferred browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzpemalnrg30zv05frxni.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzpemalnrg30zv05frxni.png" alt="Image description" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Now We build the App
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flutter build web
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will generate &lt;code&gt;build/web&lt;/code&gt; folder.&lt;/p&gt;
&lt;h2&gt;
  
  
  Testing the built App
&lt;/h2&gt;

&lt;p&gt;To test if the build is successful, you can use &lt;code&gt;live-server&lt;/code&gt;.First, install &lt;code&gt;live-server&lt;/code&gt; globally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--global&lt;/span&gt; live-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then navigate to the build directory and start the server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;build/web
live-server &lt;span class="nb"&gt;.&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now You'll see the same result as when you ran &lt;code&gt;flutter run -d web-server&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Note : &lt;strong&gt;you'll see &lt;code&gt;index.html&lt;/code&gt; in &lt;code&gt;build/web&lt;/code&gt;.Maybe you'll try &lt;code&gt;live-server index.html&lt;/code&gt;.You may see an empty page.Ensure you run &lt;code&gt;live-server&lt;/code&gt; from the &lt;code&gt;build/web&lt;/code&gt; directory without specifying &lt;code&gt;index.html&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is alternative? Using similar package in Dart?
&lt;/h2&gt;

&lt;p&gt;If you prefer using a package in Dart rather than &lt;code&gt;live-server&lt;/code&gt;, you can try &lt;code&gt;webdev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can install it globally with the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dart pub global activate webdev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then in you project folder,run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;webdev serve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Troubleshooting &lt;code&gt;webdev&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If you encounter problems when trying &lt;code&gt;webdev&lt;/code&gt;,It might be because the global installation is saved in the &lt;code&gt;.pub-cache&lt;/code&gt; folder.You need to add the following path to your &lt;code&gt;.zshrc&lt;/code&gt; or &lt;code&gt;.bashrc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;:&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HOME&lt;/span&gt;&lt;span class="s2"&gt;/.pub-cache/bin"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Still having problem ?
&lt;/h2&gt;

&lt;p&gt;If your CLI tool indicates that you need a dependency on &lt;code&gt;build_runner&lt;/code&gt; and &lt;code&gt;build_web_compilers&lt;/code&gt; in &lt;code&gt;pubspec.yaml&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can edit &lt;code&gt;pubspec.yaml&lt;/code&gt; and retry this.&lt;br&gt;
Here is an example of &lt;code&gt;pubspec.yaml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: your_project_name
description: A new Flutter project

publish_to: 'none' # Remove this line if you want to publish to pub.dev

version: 1.0.0+1

environment:
  sdk: "&amp;gt;=2.12.0 &amp;lt;3.0.0"

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2

dev_dependencies:
  flutter_test:
    sdk: flutter

  build_runner: ^2.4.0 // add this to pubspec.yaml
  build_web_compilers: ^4.0.4 // and then add this to pubspec.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now try &lt;code&gt;webdev serve&lt;/code&gt; again.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>flutter</category>
      <category>beginners</category>
      <category>liveserver</category>
    </item>
  </channel>
</rss>
