Android接入Demo,每次打开flutter页面会走两次FlutterBoost.init,第一次context里获取到的屏幕尺寸是0
Steps to Reproduce
A small application to reproduce the bug(最小化可复现的demo)
** example/lib/main.dart文件**
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Boost example',
builder: FlutterBoost.init(postPush: _onRoutePushed,builder: (BuildContext context, Widget child) {
print("Screen初始化进来了.....");
MediaQueryData mediaQuery = MediaQuery.of(context);
print("Screen初始化进来了..mediaQuery.." + mediaQuery.toString());
print("Screen初始化进来了..mediaQuery.size.." + mediaQuery.size.toString());
print("Screen初始化进来了..mediaQuery.size.width.." + mediaQuery.size.width.toString());
print("Screen初始化进来了..mediaQuery.size.height.." + mediaQuery.size.height.toString());
return child;
}),
home: Container());
}
- 官方Demo,FlutterBoost.init初始化处添加相应获取屏幕尺寸代码
- 打开Demo,清空日志,点击open flutter page按钮,查看run log日志
- 可能第一次打开,初始化只走一次(小概率);反复打开页面,返回再打开,就会一直初始化两次。第一次的初始化就会出现屏幕尺寸是0的情况;
Flutter Boost Version 0.1.66 Target Platform: Android Target OS version/browser: Android 8.0.0 Devices: 小米Mix2
Logs
I/openPageByUrl(12475): sample://flutterPage D/FlutterActivityAndFragmentDelegate(12475): Setting up FlutterEngine. D/ViewContentFactory(12475): initViewContentFetcherClass I/ContentCatcher(12475): ViewContentFetcher : ViewContentFetcher D/ViewContentFactory(12475): createInterceptor took 0ms I/flutter (12475): FlutterBoost#onMetohdCall didInitPageContainer I/flutter (12475): FlutterBoost#BoostContainerLifeCycleObserver container:flutterPage lifeCycle:ContainerLifeCycle.Init E/FlutterBoost#(12475): BoostFlutterView onAttach D/FlutterView(12475): Attaching to a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@827ec1f I/flutter (12475): FlutterBoost#onMetohdCall didShowPageContainer I/flutter (12475): FlutterBoost#ContainerObserver#2 didPush I/flutter (12475): FlutterBoost#BoostContainerLifeCycleObserver container:flutterPage lifeCycle:ContainerLifeCycle.Appear I/flutter (12475): FlutterBoost#native containner did show, I/flutter (12475): manager dump: I/flutter (12475): onstage#: I/flutter (12475): {uniqueId=1582088036715-161321102,name=flutterPage} I/flutter (12475): offstage#: I/flutter (12475): {uniqueId=default,name=default} E/ImmerseTheme(12475): current MANUFACTURER=xiaomi E/ImmerseTheme(12475): setMIUISetStatusBarLightMode MIUI > 9 I/flutter (12475): Screen初始化进来了..... I/flutter (12475): Screen初始化进来了..mediaQuery..MediaQueryData(size: Size(0.0, 0.0), devicePixelRatio: 3.0, textScaleFactor: 1.0, platformBrightness: Brightness.light, padding: EdgeInsets.zero, viewPadding: EdgeInsets.zero, viewInsets: EdgeInsets.zero, physicalDepth: 1.7976931348623157e+308, alwaysUse24HourFormat: true, accessibleNavigation: false, disableAnimations: false, invertColors: false, boldText: false) I/flutter (12475): Screen初始化进来了..mediaQuery.size..Size(0.0, 0.0) I/flutter (12475): Screen初始化进来了..mediaQuery.size.width..0.0 I/flutter (12475): Screen初始化进来了..mediaQuery.size.height..0.0 I/flutter (12475): FlutterBoost#ManagerNavigatorObserver didPush I/flutter (12475): flutterPage params:{test2: v_test2, test1: v_test1} I/flutter (12475): FlutterBoost#build widget:FlutterRouteWidget for page:flutterPage(1582088036715-161321102) I/flutter (12475): FlutterBoost#onShownContainerChanged old:default now:1582088036715-161321102 I/flutter (12475): Screen初始化进来了..... I/flutter (12475): Screen初始化进来了..mediaQuery..MediaQueryData(size: Size(360.0, 720.0), devicePixelRatio: 3.0, textScaleFactor: 1.0, platformBrightness: Brightness.light, padding: EdgeInsets(0.0, 27.7, 0.0, 0.0), viewPadding: EdgeInsets(0.0, 27.7, 0.0, 0.0), viewInsets: EdgeInsets.zero, physicalDepth: 1.7976931348623157e+308, alwaysUse24HourFormat: true, accessibleNavigation: false, disableAnimations: false, invertColors: false, boldText: false) I/flutter (12475): Screen初始化进来了..mediaQuery.size..Size(360.0, 720.0) I/flutter (12475): Screen初始化进来了..mediaQuery.size.width..360.0 I/flutter (12475): Screen初始化进来了..mediaQuery.size.height..720.0
➜ ~ flutter doctor -v
[✓] Flutter (Channel stable, v1.9.1+hotfix.6, on Mac OS X 10.12.5 16F73, locale
zh-Hans-CN)
• Flutter version 1.9.1+hotfix.6 at /Users/user/flutter
• Framework revision 68587a0916 (5 months ago), 2019-09-13 19:46:58 -0700
• Engine revision b863200c37
• Dart version 2.5.0
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at /Users/user/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling
support)
• Platform android-29, build-tools 29.0.2
• Java binary at:
/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bin/java
• Java version Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
! Some Android licenses not accepted. To resolve this, run: flutter doctor
--android-licenses
[✗] Xcode - develop for iOS and macOS
✗ Xcode installation is incomplete; a full installation is necessary for iOS
development.
Download at: https://developer.apple.com/xcode/download/
Or install Xcode via the App Store.
Once installed, run:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
✗ CocoaPods not installed.
CocoaPods is used to retrieve the iOS and macOS platform side's plugin
code that responds to your plugin usage on the Dart side.
Without CocoaPods, plugins will not work on iOS or macOS.
For more info, see https://flutter.dev/platform-plugins
To install:
sudo gem install cocoapods
[!] Android Studio (not installed)
• Android Studio not found; download from
https://developer.android.com/studio/index.html
(or visit https://flutter.dev/setup/#android-setup for detailed
instructions).
[✓] IntelliJ IDEA Ultimate Edition (version 2019.2.2)
• IntelliJ at /Applications/IntelliJ IDEA.app
• Flutter plugin version 40.2.3
• Dart plugin version 192.7402
[✓] Connected device (1 available)
• MIX 2 • 840612b9 • android-arm64 • Android 8.0.0 (API 26)
! Doctor found issues in 3 categories.
@MuJie hello,这应该是build函数可能被调用多次。 这问题应该根flutter官方设计有关,详情可参考这2个官方issue https://github.com/flutter/flutter/issues/25827 、https://github.com/flutter/flutter/issues/23339#issuecomment-432745227 经测试,在纯净的Flutter项目中(未含boost),debug模式不会(不易?)触发此情况,release模式大概率触发此情况。原因是release时性能会更快,native状态信息同步到dart层有一定延迟。建议主动handle width/height 0/0的情况。
具体到boost中的原理,MediaQuery.of(context).size的来源取决于XFlutterView.sendViewportMetricsToFlutter方法, 该值的width/height需要等到XFlutterView.onSizeChanged才会得以更新。
cc @noborder
@Firewayer 恩,我们已经找到了解决方法,具体ScreenUtil相关的初始化放到了更贴近页面打开的地方。
看看是不是application的oncreate调用了两次,如果有推送进程的话oncreate会调用两次,我是加了个是主进程的判断
@Firewayer 恩,我们已经找到了解决方法,具体ScreenUtil相关的初始化放到了更贴近页面打开的地方。
请问你放在哪个位置的?我也是这个情况
两次
老哥,请问下怎么加主进程判断,我现在也遇到了这个问题
这个问题我在 flutter 1.22.4 还有。。。我的解决方案:
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth != 0) {
return MaterialApp(
home: HomePage()
}
return Container();
},
);
}
参考了:https://github.com/OpenFlutter/flutter_screenutil/blob/20ac1de0f2f2b95d43922d1567fa2aa1629c8cee/lib/screenutil_init.dart#L22.
class Test extends StatefulWidget { final Widget child;
const Test({Key? key, required this.child}) : super(key: key);
@override
State
class _TestState extends State
@override Widget build(BuildContext context) { h = MediaQuery.of(context).size.height; if (h == 0) { return widget.child;
}
//这里初始化 自己想要的东西 比如toast 等 return Container( color: Colors.lightGreen, height: 100, width: 100, child: widget.child, ); }
@override void didUpdateWidget(covariant Test oldWidget) { if (h == 0.0) setState(() { log('我重新跟新了'); }); } }
把 appBuilder的child 传递进来