Use flutter web widgets inside a react js app
Asked Answered
B

3

10

I have this project where I have to code a website and ios and android apps.

I have to do this with a very limited team (basically myself). So I want to share as much code between those platforms, to avoid maintaining different codebases as much as possible.

I have come to consider flutter : the logic (and interaction with the backend) in dart can be compiled to both ios and android, and to javascript for the web site to call. I also like how the UI is built using flutter.

For the web site, I know there is flutter web that can be used to do a web app, but I want my website to feel like a web site more than a single page app. I have thought about using React+Gatsby to do the website's ui, while calling the dart compiled js for the logic. That would enable high page loading speed, good seo, while keeping the interactivity of a react app and the single codebase logic through dart to js compilation.

I am wondering if this seems like a good approach to you, and if not, how you would do it.

In this approach, I am wondering if it would be possible to embedded flutter web widgets inside of react js components, always in a concern of maintaining as low platform specific code as possible. I have not found any other way of doing this than embedding them into iframes, which does not seem like a good idea, or does it ?

Blackguard answered 23/12, 2020 at 1:13 Comment(1)
I am wondering what approach you took to solve this?Translative
C
3

Update

Flutter Web is now available in a stable version for production.

enter image description here

Below answer is relevant back to the time when question was asked.

Flutter Web not recommended

At the current date, I would recommend not to use Flutter Web for production as it's in Beta. The Flutter Team is still working on improving quality, performance and browser compatibility. You must be cautious about using it as you may run into bugs and some other complications.

Also, I believe that the community support for Flutter Web might be on a lower side as it's pretty new.

Limited browser support

Another factor which bugs me is that the Flutter web apps can run on limited browsers as of now which would affect the reach to your user base:

  • Chrome (mobile & desktop)
  • Safari (mobile & desktop)
  • Edge (mobile & desktop)
  • Firefox (desktop)

What would I prefer?

Well, although it might be a hassle to handle different codebases for mobile and web platforms, I would still prefer to stick with React JS as it's much stable with better browser & community support as opposed to Flutter Web.

Good luck with your app! :)

Copilot answered 28/12, 2020 at 19:37 Comment(0)
P
1

I am unaware of how to use Flutter widgets inside a React app. But to address your other concerns,

So I want to share as much code between those platforms, to avoid maintaining different codebases as much as possible.

Given no other restrictions, this leads to a single Flutter app for both web and native. For a team as small as three we still found it easier than having multiple projects.

The key factor in merging our initial projects to one was the notion of mobile browsers. In a mobile browser you website should reduce itself to the look of your mobile app, unless they have different purposes. And this automatically happens with Flutter projects if you derive your layout breakpoints from screen size. Otherwise you would code your same narrow design twice: in Flutter for native apps and in React for web.

Same goes for native apps for tablets in landscape mode. They call for a layout that resembles your desktop browser version, and you automatically get it with Flutter.

Also in web version you may have more tools than in a native app, just because your screen allows it. Naturally, you would code this logic in JS. But then you get an order to migrate this to native apps. Would you then replace your JS logic with Dart logic compiled to JS and embedded into frames? This is a messy road.

Lastly, think of Mac, Linux and Windows platforms that will come to Flutter soon. Should you ever need a desktop native app, you would want the same layout as with web.

I want my website to feel like a web site more than a single page app.

The difference in feeling between a website and a single page app lies mostly in state management and URL handling. You may do the following to reduce it:

  • Introduce URLs for your pages. By default, in Flutter every piece of action can happen under a single URL of example.com/#/. The window then feels fragile as there is no URL to copy and get back to. Flutter's Router API, released in Oct 2020, allows you to generate URLs on any change in your app's state and parse the state back from URL. If you do it right, then everything on your screen becomes a function of the URL, just like in traditional websites.
  • Use url_stragegy package for your URLs to be example.com/path rather than example.com/#/path which immediately feels fake.

I have not found any other way of doing this than embedding them into iframes, which does not seem like a good idea, or does it ?

No, it does not. Mostly because you get too narrow a channel of communication of messages between JS windows. How would you listen to Flutter's BLoC's stream in another frame? It would take too much boilerplate code. Also I cannot think of easy debugging process.

To me, Flutter Web still has drawbacks:

  • It takes awhile to load.
  • Many specific JS APIs are not implemented yet.
  • Many services you integrate with may not have Dart SDK, while having JS SDK. For instance, I struggled to get Ably working.
  • No search engine optimization.
  • You cannot use em as a screen unit, so scaling elements may get tricky.

Still with limited resources a single codebase outweights everything else.

To be clear, my team has been working on a web+native project the whole 2021. It now has passed most of the testing, but not yet released. For a sense of scale, it is a marketplace of 40+ screens.

Parallelogram answered 28/11, 2021 at 10:58 Comment(0)
M
0

I'm currently evaluating flutter for a new project and have the same question.

I've found much of the content here (and elsewhere!) somewhat dated and incomplete.

This has been possible since v3.10:

You can embed a Flutter web app into any HTML element of your web page, with flutter.js and the hostElement engine initialization parameter.

See the docs: Embedding a Flutter app into an HTML page

e.g.

<html>
  <head>
    <!-- ... -->
    <script src="flutter.js" defer></script>
  </head>
  <body>

    <!-- Ensure your flutter target is present on the page... -->
    <div id="flutter_host">Loading...</div>

    <script>
      window.addEventListener("load", function (ev) {
        _flutter.loader.loadEntrypoint({
          onEntrypointLoaded: async function(engineInitializer) {
            let appRunner = await engineInitializer.initializeEngine({
              // Pass a reference to "div#flutter_host" into the Flutter engine.
              hostElement: document.querySelector("#flutter_host")
            });
            await appRunner.runApp();
          }
        });
      });
    </script>
  </body>
</html>

Mccormac answered 6/9, 2023 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.