1

The error is as follows

reason

We use ScreenUtil().screenWidth in the layout to obtain the screen width. When initializing the unloaded view, it cannot be obtained the first time it is obtained, and the above crash will occur.

solution

import 'package:flutter_screenutil/screenutil_init.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
@override
Widget build(BuildContext context) {
  //使用屏幕初始化包裹整个body布局
  return ScreenUtilInit(
    //设置初始化屏幕大小
    designSize: Size(375, 812),
    builder: () => Scaffold(
      backgroundColor: WBColors.color_f4f5f7,
      body: Container(
          height: 263.5,
          width: ScreenUtil().screenWidth,
        )
    )
  );
}
  • Use ScreenUtilInit screen initialization component to wrap the outermost layer of the entire page layout
  • Set the initial size of the screen
  • The API for obtaining the width and height of the screen can be used arbitrarily in the body

兰俊秋雨
5.1k 声望3.5k 粉丝

基于大前端端技术的一些探索反思总结及讨论