Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure to initialize ScreenUtil before accessing it #196

Closed
mehmetyilmaz001 opened this issue Jan 7, 2021 · 12 comments
Closed

Ensure to initialize ScreenUtil before accessing it #196

mehmetyilmaz001 opened this issue Jan 7, 2021 · 12 comments

Comments

@mehmetyilmaz001
Copy link

Hi all,

I am trying to implement flutter_screenutil like this in my project

`Widget build(BuildContext context) {

return MultiBlocProvider(
  providers: [

  ],
  child: StyledToast(

    child: ScreenUtilInit(
      designSize: Size(375, 812),
      allowFontScaling: true,
      child: MaterialApp(
          title: 'Material App',`

Bu when I restart the app in debug mode I got this error.

Ensure to initialize ScreenUtil before accessing it. Please execute the init method : ScreenUtil.init() 'package:flutter_screenutil/screenutil.dart': Failed assertion: line 33 pos 7: '_instance != null'

Bu when I Hot Reload the app the error is not showing.

I am using the version 4.0.2+3
Flutter version: 1.22.5

I attached a video about the error.

Thanks.

aa.mov
@lizhuoyuan
Copy link
Collaborator

Maybe you should rerun

@lakerszhy
Copy link

You can put MaterialApp in another widget.

@BelongsH
Copy link

return ScreenUtilInit(
      designSize: Size(375, 812),
      allowFontScaling: false,
      child: MaterialApp(
        title: '测试',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primarySwatch: Colors.blue,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: SearchPage(),
      ),
    );

不知道什么原因,直接使用ScreenUtilInit,里面的build函数会比SearchPage里面包含sp的属性,执行的时机慢。就会出现上面的错误。

如果自己包装的话,就不会

  return LayoutBuilder(
      // ignore: missing_return
      builder: (_, BoxConstraints constraints) {
        if (constraints.maxWidth != 0) {
          ScreenUtil.init(
            constraints,
            designSize: Size(375, 812),
            allowFontScaling: false,
          );

          return MaterialApp(
            title: '测试',
            debugShowCheckedModeBanner: false,
            theme: ThemeData(
              primarySwatch: Colors.blue,
              visualDensity: VisualDensity.adaptivePlatformDensity,
            ),
            home: SearchPage(),
          );
        }
      },
    );

我很困惑。

@jasonlaw
Copy link

jasonlaw commented Jan 19, 2021

Same problem here.
The workaround by @BelongsH fixed the issue.
Updated: just encountered the error again with the workaround, so it is not fixing the issue. @lizhuoyuan please take a look into this. Thank you.

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building LayoutBuilder:
A build function returned null.

The offending widget is: LayoutBuilder
Build functions must never return null.

To return an empty space that causes the building widget to fill available room, return "Container()". To return an empty space that takes as little room as possible, return "Container(width: 0.0, height: 0.0)".

When the exception was thrown, this was the stack
#0 debugWidgetBuilderValue.
package:flutter/…/widgets/debug.dart:302
#1 debugWidgetBuilderValue
package:flutter/…/widgets/debug.dart:323
#2 _LayoutBuilderElement._layout.
package:flutter/…/widgets/layout_builder.dart:125
#3 BuildOwner.buildScope
package:flutter/…/widgets/framework.dart:2683
#4 _LayoutBuilderElement._layout
package:flutter/…/widgets/layout_builder.dart:120
...
══════════════════════════════════════════════════════════════════════════

@lizhuoyuan
Copy link
Collaborator

@jasonlaw ok, I know, thank you

lizhuoyuan pushed a commit that referenced this issue Jan 25, 2021
@jing-pei
Copy link

@lizhuoyuan
flutter_screenutil: ^4.0.2+8
问题依然存在

@jing-pei
Copy link

这个应该是flutter对Build Widget更换了处理时机,建议去Flutter 提交一个issue

@a1005342094
Copy link

I've also met that after packing, some devices will be directly grayed out when they load slowly

@lizhuoyuan
Copy link
Collaborator

It seems I need to study this issue carefully

@lizhuoyuan
Copy link
Collaborator

I've also met that after packing, some devices will be directly grayed out when they load slowly

please try 4.0.3

@IOCloudFly
Copy link

我也遇到的同样的问题,没解决

@idootop
Copy link
Contributor

idootop commented Feb 5, 2021

return ScreenUtilInit(
      designSize: Size(375, 812),
      allowFontScaling: false,
      child: MaterialApp(
        title: '测试',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primarySwatch: Colors.blue,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: SearchPage(),
      ),
    );

不知道什么原因,直接使用ScreenUtilInit,里面的build函数会比SearchPage里面包含sp的属性,执行的时机慢。就会出现上面的错误。

如果自己包装的话,就不会

  return LayoutBuilder(
      // ignore: missing_return
      builder: (_, BoxConstraints constraints) {
        if (constraints.maxWidth != 0) {
          ScreenUtil.init(
            constraints,
            designSize: Size(375, 812),
            allowFontScaling: false,
          );

          return MaterialApp(
            title: '测试',
            debugShowCheckedModeBanner: false,
            theme: ThemeData(
              primarySwatch: Colors.blue,
              visualDensity: VisualDensity.adaptivePlatformDensity,
            ),
            home: SearchPage(),
          );
        }
      },
    );

我很困惑。

原因很简单,ScreenUtilInit里的child是传过来的参数,相当于提前build好的child widget,把这个提前build好的widget放到LayoutBuilder里的效果可想而知

如果你自己在外面放一个LayoutBuilder,然后把child widget放到builder里当然就不会出现child widegt的尺寸没有初始化的问题

所以这个问题的解决方法也很简单,就是把传过来的child widget换成builder

Snipaste_2021-02-05_11-16-48

lizhuoyuan added a commit that referenced this issue Feb 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants