Introduction
Flutter web brings your Flutter apps to the browser, but the web platform introduces unique challenges: responsive layouts, persistent storage, code splitting, and SEO considerations. BLoC works seamlessly on the web, but you need to adapt some patterns to leverage web capabilities. This guide covers everything you need to know to build robust, performant Flutter web applications using BLoC.
BLoC Works Everywhere
BLoC is platform‑agnostic. The same BLoC classes you write for mobile will work on web without changes. However, certain features (like local storage, responsive design, and routing) require web‑specific implementations. This guide focuses on those differences.
Setting Up Flutter Web with BLoC
Enable web support in your Flutter project:
Add flutter_bloc to pubspec.yaml. The rest of the BLoC setup is identical to mobile.
State Persistence on the Web
Unlike mobile, web browsers provide different storage mechanisms: localStorage (synchronous, up to ~5-10MB), IndexedDB (asynchronous, larger capacity), and sessionStorage. hydrated_bloc works on web out of the box using localStorage via shared_preferences or a custom adapter.
If you need IndexedDB for larger datasets, consider using hive with a custom backend or sqflite via sqflite_common_ffi (not recommended for web). For simple settings, shared_preferences (which uses localStorage on web) is sufficient.
Responsive UI with BLoC
Web apps need to adapt to different screen sizes. Use BLoC to manage layout state (e.g., breakpoints, sidebars). A common pattern is to listen to MediaQuery and update a BLoC state.
Code Splitting & Lazy Loading
For large web apps, you can split your code into chunks that load only when needed. Use Dart's deferred imports. BLoC can be integrated with this pattern by lazy‑registering BLoCs in the route.
Navigation and Routing
Flutter web supports URL‑based navigation. Use go_router or auto_route with BLoC. The BLoC pattern remains unchanged; the router simply triggers events or provides initial arguments to BLoCs via provider.
Handling Web-Specific Events
Web apps need to listen to scroll, resize, and visibility changes. You can use dart:html (carefully, with conditional imports) or Flutter's built‑in widgets. BLoC can react to these events.
SEO Considerations
Flutter web apps are client‑rendered, which can impact SEO. BLoC doesn't directly affect SEO, but you should ensure your app can be indexed by using server‑side rendering (if needed) or dynamic metadata. For SEO‑critical content, consider using flutter_html or rendering key content on the server. You can still manage SEO tags (e.g., <title> and <meta>) using the flutter_html or the flutter_inappwebview for dynamic updates.
Performance Optimizations
- Use
constconstructors – Reduces widget rebuilds. - Lazy load BLoCs – Use
BlocProviderwithcreateinstead of instantiating all BLoCs at startup. - Avoid heavy computations in event handlers – Offload to isolates or use
compute. - Use
buildWhen– Prevent unnecessary rebuilds on the web. - Minify and gzip – When deploying, use
flutter build web --releaseand serve compressed assets. - Consider using
CanvasKitorHTMLrenderer – Choose based on your performance needs (CanvasKit for consistent rendering, HTML for smaller bundle).
Deployment
Build your web app with:
Serve the files with any static server. For advanced hosting, you can integrate with Firebase Hosting, Netlify, or Vercel.
Best Practices
- Write platform‑agnostic BLoCs – Keep business logic separate from platform‑specific code.
- Use conditional imports for web‑only code – Use
dart:htmlonly when necessary and guard withkIsWeb. - Test on multiple browsers – Chrome, Firefox, Safari, Edge.
- Monitor performance with Lighthouse – Use DevTools Lighthouse tab to measure web vitals.
- Handle offline support – Use service workers (via
flutter_web_plugins) to cache assets, but BLoC can manage the state.
Common Mistakes
- ❌ Using
dart:ioon web – Causes runtime errors. ✅ Use conditional imports or package:universal_io. - ❌ Storing large data in
localStorage– Exceeds storage limits. ✅ Use IndexedDB viahiveorsqflite_common_ffi. - ❌ Not testing on slow networks – Web apps may load slowly on 3G. ✅ Optimize assets and use code splitting.
- ❌ Ignoring responsive design – App may look broken on desktop. ✅ Use BLoC to manage breakpoints.
Conclusion
BLoC is a powerful state management solution for Flutter web. By adapting your architecture to web‑specific concerns—responsive layouts, web storage, code splitting—you can build fast, maintainable web applications. Use the patterns and best practices in this guide to ensure your app runs smoothly across all browsers.