Skip to content

使用ScreenUtilInit运行报错:Ensure to initialize ScreenUtil before accessing it. #260

@lizhangjun23

Description

@lizhangjun23

版本:4.0.4+1以及其他4.0.X其他版本
错误:======== Exception caught by widgets library =======================================================
The following assertion was thrown building LayoutBuilder:

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

The relevant error-causing widget was:
OrientationBuilder file:///D:/tool/flutter-env/flutter_1.22.6/.pub-cache/hosted/pub.flutter-io.cn/flutter_screenutil-4.0.4+1/lib/screenutil_init.dart:25:14
When the exception was thrown, this was the stack:
#2 new ScreenUtil (package:flutter_screenutil/screen_util.dart:34:7)
#3 Base.dp (package:qniqni/common/base.dart:14:36)
#4 _StartPageState.build. (package:qniqni/page/init/start.dart:81:26)
#5 ScreenUtilInit.build.. (package:flutter_screenutil/screenutil_init.dart:35:25)
#6 OrientationBuilder._buildWithConstraints (package:flutter/src/widgets/orientation_builder.dart:52:19)
代码:
@OverRide
Widget build(BuildContext context) {
return ScreenUtilInit(
designSize: Base.size,
builder: () {
return Scaffold(
body: SizedBox(
width: MediaQuery.of(context).size.width, //获取屏幕宽度
child: Stack(
alignment: Alignment.center,
children: [
SizedBox(
width: dp(375),
child: Image.asset("assets/start.jpg"),
),
Positioned(
right: dp(40),
top: dp(40),
child: CountDown(),
),
],
),
),
);
},
);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions