简体   繁体   English

Flutter Web 应用程序无法在移动浏览器上运行。 如果在移动浏览器上运行flutter代码,如何调试错误?

[英]Flutter web app not working on mobile browser. How to debug the error if running flutter code on mobile browser?

flutter web app is working fine on desktop chrome and safari browser in debug and release mode. Flutter Web 应用程序在调试和发布模式下在桌面 chrome 和 safari 浏览器上运行良好。 But, it is not working on mobile browser in debug and release mode.但是,它不适用于处于调试和发布模式的移动浏览器。 I uploaded the release mode files on server and try to access to the website using URL but one page is not working fine.我在服务器上上传了发布模式文件并尝试使用 URL 访问该网站,但有一页无法正常工作。 Widget Design completely damage. Widget 设计完全损坏。

How to debug where is error if running flutter code on mobile browser?如果在移动浏览器上运行flutter代码,如何调试错误在哪里? see below screen shot.请参阅下面的屏幕截图。

在此处输入图片说明

This is pretty common error in Flutter Web, don't worry it's easy to fix.这是 Flutter Web 中非常常见的错误,别担心它很容易修复。 I spent a lot of days behind this errors to find the solution as I don't know how to search on stackoverflow.com because of cannot finding a error message or debug message in release mode.我花了很多天来解决这个错误,因为我不知道如何在 stackoverflow.com 上搜索,因为在发布模式下找不到错误消息或调试消息。

Actually that is not true and you can read the debug console easily by getting into the inspect tab in chrome.实际上,事实并非如此,您可以通过进入 chrome 中的检查选项卡轻松阅读调试控制台。

Right click on the screen > Inspect Element and you can have it.右键单击屏幕> 检查元素,您可以拥有它。

在此处输入图片说明

First Method第一种方法

Please run your app on debug mode once again and carefully watch the debug log which is printed in the VSCode debug console.请再次在调试模式下运行您的应用程序,并仔细查看 VSCode 调试控制台中打印的调试日志。 and you can find something like this.你可以找到这样的东西。

Another exception was thrown: Incorrect use of ParentDataWidget. 

or或者

flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
    flutter: The following message was thrown during layout:
    flutter: A RenderFlex overflowed by 15 pixels on the right.
    flutter:
    flutter: The overflowing RenderFlex has an orientation of Axis.horizontal.

These errors don't stop the app from running and sometimes it doesn't show up any berriers to work on app while in development.这些错误不会阻止应用程序运行,有时它不会在开发过程中显示任何运行应用程序的障碍。 especially the parent data error.尤其是父数据错误。

Once you find and fix this warning message.找到并修复此警告消息后。 You can build to the release mode and Voila.您可以构建到发布模式和瞧。 You can have your app working pretty well!您可以让您的应用程序运行良好!

Second Method第二种方法

If you cannot understand the problem from the above method.如果您无法通过上述方法理解问题。 then you can approach the this way.那么你可以通过这种方式接近。

Build your production release (like you have it already) or run the app with debug console like this构建您的生产版本(就像您已经拥有它一样)或使用这样的调试控制台运行应用程序

flutter run -d chrome --release

You can have the same look as the release mode with a grey box on the UI.您可以通过 UI 上的灰色框获得与发布模式相同的外观。 Find the place in the code where the Grey Box Appear, and you can find some Expanded() widget at that spot.在代码中找到灰框出现的位置,您可以在该位置找到一些 Expanded() 小部件。

Just Remove the expanded widget and recheck the code.只需删除扩展的小部件并重新检查代码。 If there is any render flex overflow error wrap the widget with a SizedBox or container with predefined width / height and the error will be gone.如果有任何渲染 flex 溢出错误,请使用 SizedBox 或具有预定义宽度/高度的容器包装小部件,错误将消失。

It's possible that this is happening due to some widget overflow problems in release mode.这可能是由于发布模式下的一些小部件溢出问题而发生的。 Try heading to the dev tools in your browser and switching to mobile mode to get the screen width to narrow.尝试前往浏览器中的开发工具并切换到移动模式以缩小屏幕宽度。 And please be more specific than doesn't work so that we can better help you.并且请比不起作用更具体,以便我们可以更好地为您提供帮助。

We were not shown the code.我们没有看到代码。 Therefore, it is difficult to say for sure.因此,很难确定。 But I had such problems when I ran the application building without parameter --web-renderer但是当我在没有参数--web-renderer情况下运行应用程序构建时遇到了这样的问题

flutter build web

The point was that by default it uses different renderers (CanvasKit for desktop and HTML for mobile).关键是默认情况下它使用不同的渲染器(用于桌面的 CanvasKit 和用于移动的 HTML)。 And since I used things that required CanvasKit, the application in mobile browsers was displayed incorrectly.而且由于我使用了需要 CanvasKit 的东西,因此移动浏览器中的应用程序显示不正确。

Building with the appropriate parameter solved my problem使用适当的参数构建解决了我的问题

flutter build web --web-renderer canvaskit

Details can be found here 详细信息可以在这里找到

Here is what I did这是我所做的

  1. launch android avd启动安卓AVD

  2. launch flutter app with your desktop ip and port (eg 7357) $ flutter run -d chrome --debug --web-hostname --web-port 7357使用您的桌面 IP 和端口(例如 7357)启动 flutter 应用程序 $ flutter run -d chrome --debug --web-hostname --web-port 7357

  3. launch a chrome on avd and enter : eg 192.168.10.4:7357在 avd 上启动 chrome 并输入:例如 192.168.10.4:7357

  4. open a desktop chrome and enter the following in the address bar: chrome://inspect/#devices打开桌面 chrome 并在地址栏中输入以下内容:chrome://inspect/#devices

There you can find the one you set : like this:在那里你可以找到你设置的那个:像这样: 在此处输入图片说明

  1. Click "inspect" then a new debug window appears!单击“检查”,然后会出现一个新的调试窗口!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM