How to Build a Flutter Web App: Step-by-Step Guide
To build a Flutter web app, install Flutter SDK with web support, create a new project using
flutter create, and run it with flutter run -d chrome. Use Flutter widgets to build your UI, and deploy the app by compiling with flutter build web.Syntax
Flutter web apps use the same Dart language and Flutter widgets as mobile apps. The main entry point is a main() function that runs runApp() with your root widget. Use widgets like MaterialApp and Scaffold to build the UI.
Commands to create and run a web app:
flutter create my_web_app- creates a new Flutter projectcd my_web_app- go to project folderflutter run -d chrome- runs the app in Chrome browserflutter build web- builds the app for deployment
dart
void main() { runApp(MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Flutter Web App')), body: Center(child: Text('Hello, Flutter Web!')), ), )); }
Output
A web page with a top app bar titled 'Flutter Web App' and centered text 'Hello, Flutter Web!'
Example
This example shows a simple Flutter web app with a button that updates a counter when clicked.
dart
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatefulWidget { const MyApp({super.key}); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { int _count = 0; void _increment() { setState(() { _count++; }); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Flutter Web Counter')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text('You clicked the button this many times:'), Text('$_count', style: const TextStyle(fontSize: 40)), ElevatedButton( onPressed: _increment, child: const Text('Click me'), ), ], ), ), ), ); } }
Output
A web page with an app bar titled 'Flutter Web Counter', text showing the number of clicks starting at 0, and a button labeled 'Click me' that increments the count when pressed.
Common Pitfalls
Common mistakes when building Flutter web apps include:
- Not enabling web support in Flutter SDK (run
flutter config --enable-webif needed) - Trying to run on unsupported browsers or devices
- Using mobile-only plugins that don't support web
- Ignoring responsive design, causing UI to look bad on different screen sizes
- Not testing with
flutter run -d chromebefore building
dart
/* Wrong: Using a plugin that only works on mobile */ // import 'package:some_mobile_only_plugin/some_mobile_only_plugin.dart'; /* Right: Use web-compatible plugins or conditional imports */ // import 'package:universal_html/html.dart' as html; /* Wrong: Running without enabling web support */ // flutter run /* Right: Enable web and run on Chrome */ // flutter config --enable-web // flutter run -d chrome
Quick Reference
Key commands and tips for Flutter web development:
| Command | Description |
|---|---|
| flutter create my_web_app | Create a new Flutter project |
| flutter run -d chrome | Run the app in Chrome browser |
| flutter build web | Build the app for production deployment |
| flutter config --enable-web | Enable web support in Flutter SDK |
| Use MaterialApp and Scaffold | Basic widgets for web UI structure |
Key Takeaways
Flutter web apps use the same Dart and widget code as mobile apps.
Run your app in Chrome with 'flutter run -d chrome' to test web features.
Enable web support with 'flutter config --enable-web' if not already enabled.
Avoid mobile-only plugins; use web-compatible packages for best results.
Build the final web app with 'flutter build web' for deployment.