fix(runtime): late top-level/instance vars used with ??= (mediaQuery/screenHeight/etc) -> nullable. Fixes LateInitializationError on /me /shop /product-detail

This commit is contained in:
2026-08-01 02:06:07 +08:00
parent 0526baa3c8
commit 147ab866e1
4 changed files with 41 additions and 41 deletions

View File

@@ -16,9 +16,9 @@ import '../../utils/util_web.dart'
if (dart.library.io) '../../utils/util_io.dart'; if (dart.library.io) '../../utils/util_io.dart';
import '../../utils/utils.dart'; import '../../utils/utils.dart';
late MediaQueryData mediaQuery; MediaQueryData? mediaQuery;
late double statusBarHeight; double? statusBarHeight;
late double screenHeight; double? screenHeight;
class DesktopMe extends StatefulWidget { class DesktopMe extends StatefulWidget {
final Key? key; final Key? key;
@@ -67,8 +67,8 @@ class DesktopMeState extends State<DesktopMe> {
} }
mediaQuery ??= MediaQuery.of(context); mediaQuery ??= MediaQuery.of(context);
screenHeight ??= mediaQuery.size.height; screenHeight ??= mediaQuery!.size.height;
statusBarHeight ??= mediaQuery.padding.top; statusBarHeight ??= mediaQuery!.padding.top;
Widget userInfo = GestureDetector( Widget userInfo = GestureDetector(
child: Container( child: Container(

View File

@@ -20,9 +20,9 @@ import '../../utils/util_web.dart'
if (dart.library.io) '../../utils/util_io.dart'; if (dart.library.io) '../../utils/util_io.dart';
import '../../utils/utils.dart'; import '../../utils/utils.dart';
late MediaQueryData mediaQuery; MediaQueryData? mediaQuery;
late double statusBarHeight; double? statusBarHeight;
late double screenHeight; double? screenHeight;
class MobileMe extends StatefulWidget { class MobileMe extends StatefulWidget {
final Key? key; final Key? key;
@@ -43,7 +43,7 @@ class MobileMeState extends State<MobileMe> {
User? _user; User? _user;
late ShopScrollCoordinator _shopCoordinator; late ShopScrollCoordinator _shopCoordinator;
late ShopScrollController _pageScrollController; ShopScrollController? _pageScrollController;
final double _sliverAppBarInitHeight = 165.0; final double _sliverAppBarInitHeight = 165.0;
final double _appBarHeight = 85.0; final double _appBarHeight = 85.0;
@@ -65,13 +65,13 @@ class MobileMeState extends State<MobileMe> {
} }
mediaQuery ??= MediaQuery.of(context); mediaQuery ??= MediaQuery.of(context);
screenHeight ??= mediaQuery.size.height; screenHeight ??= mediaQuery!.size.height;
statusBarHeight ??= mediaQuery.padding.top; statusBarHeight ??= mediaQuery!.padding.top;
_pageScrollController ??= _pageScrollController ??=
_shopCoordinator.pageScrollController(_sliverAppBarInitHeight * -1.0); _shopCoordinator.pageScrollController(_sliverAppBarInitHeight * -1.0);
_shopCoordinator.pinnedHeaderSliverHeightBuilder ??= () { _shopCoordinator.pinnedHeaderSliverHeightBuilder ??= () {
return statusBarHeight + kToolbarHeight + _appBarHeight; return statusBarHeight! + kToolbarHeight + _appBarHeight;
}; };
_listScrollController1 = _shopCoordinator.newChildScrollController(); _listScrollController1 = _shopCoordinator.newChildScrollController();

View File

@@ -23,10 +23,10 @@ import '../../widgets/general/add_remove_button.dart';
import '../../widgets/general/carousel.dart'; import '../../widgets/general/carousel.dart';
import '../../widgets/general/show_price.dart'; import '../../widgets/general/show_price.dart';
late MediaQueryData mediaQuery; MediaQueryData? mediaQuery;
late double statusBarHeight; double? statusBarHeight;
late double screenHeight; double? screenHeight;
late double screenWidth; double? screenWidth;
class MobileProductDetailPage extends StatefulWidget { class MobileProductDetailPage extends StatefulWidget {
final Business business; final Business business;
@@ -45,12 +45,12 @@ class MobileProductDetailPage extends StatefulWidget {
class MobileProductDetailPageState extends State<MobileProductDetailPage> class MobileProductDetailPageState extends State<MobileProductDetailPage>
with SingleTickerProviderStateMixin { with SingleTickerProviderStateMixin {
late ShopScrollCoordinator _shopCoordinator; late ShopScrollCoordinator _shopCoordinator;
late ShopScrollController _pageScrollController; ShopScrollController? _pageScrollController;
late TabController _tabController; late TabController _tabController;
late double _sliverAppBarInitHeight; double? _sliverAppBarInitHeight;
late double _sliverAppBarMaxHeight; double? _sliverAppBarMaxHeight;
final double _tabBarHeight = 50; final double _tabBarHeight = 50;
ProductDetail? productDetail; ProductDetail? productDetail;
@@ -75,18 +75,18 @@ class MobileProductDetailPageState extends State<MobileProductDetailPage>
refresh = false; refresh = false;
mediaQuery ??= MediaQuery.of(context); mediaQuery ??= MediaQuery.of(context);
screenHeight ??= mediaQuery.size.height; screenHeight ??= mediaQuery!.size.height;
screenWidth ??= mediaQuery.size.width; screenWidth ??= mediaQuery!.size.width;
statusBarHeight ??= mediaQuery.padding.top; statusBarHeight ??= mediaQuery!.padding.top;
_sliverAppBarInitHeight ??= screenWidth; _sliverAppBarInitHeight ??= screenWidth;
_sliverAppBarMaxHeight ??= screenWidth; _sliverAppBarMaxHeight ??= screenWidth;
_pageScrollController ??= _shopCoordinator _pageScrollController ??= _shopCoordinator
.pageScrollController(_sliverAppBarMaxHeight - _sliverAppBarInitHeight); .pageScrollController(_sliverAppBarMaxHeight! - _sliverAppBarInitHeight!);
_shopCoordinator.pinnedHeaderSliverHeightBuilder ??= () { _shopCoordinator.pinnedHeaderSliverHeightBuilder ??= () {
return statusBarHeight + kToolbarHeight + _tabBarHeight; return statusBarHeight! + kToolbarHeight + _tabBarHeight;
}; };
return Scaffold( return Scaffold(
@@ -110,10 +110,10 @@ class MobileProductDetailPageState extends State<MobileProductDetailPage>
}, },
), ),
backgroundColor: Theme.of(context).primaryColor, backgroundColor: Theme.of(context).primaryColor,
expandedHeight: _sliverAppBarMaxHeight - 30.0, expandedHeight: _sliverAppBarMaxHeight! - 30.0,
flexibleSpace: FlexibleSpaceBar( flexibleSpace: FlexibleSpaceBar(
background: Carousel( background: Carousel(
height: _sliverAppBarInitHeight + 0.0, height: _sliverAppBarInitHeight! + 0.0,
pages: _getProductPictures(context), pages: _getProductPictures(context),
autoPlay: false, autoPlay: false,
), ),

View File

@@ -37,10 +37,10 @@ import 'product_item.dart';
import 'product_search.dart'; import 'product_search.dart';
import 'shopping_cart_bar.dart'; import 'shopping_cart_bar.dart';
late MediaQueryData mediaQuery; MediaQueryData? mediaQuery;
late double statusBarHeight; double? statusBarHeight;
late double screenWidth; double? screenWidth;
late double screenHeight; double? screenHeight;
class Shop extends StatefulWidget { class Shop extends StatefulWidget {
final int? businessId; final int? businessId;
@@ -80,11 +80,11 @@ class ShopState extends State<Shop>
bool refresh = false; bool refresh = false;
late ShopScrollCoordinator _shopCoordinator; late ShopScrollCoordinator _shopCoordinator;
late ShopScrollController _pageScrollController; ShopScrollController? _pageScrollController;
late TabController _tabController; late TabController _tabController;
final double _sliverAppBarInitHeight = 150.0; final double _sliverAppBarInitHeight = 150.0;
final double _tabBarHeight = 50.0; final double _tabBarHeight = 50.0;
late double _sliverAppBarMaxHeight; double? _sliverAppBarMaxHeight;
ShopScrollController? _listScrollController1; ShopScrollController? _listScrollController1;
late ShopScrollController _listScrollController2; late ShopScrollController _listScrollController2;
@@ -204,17 +204,17 @@ class ShopState extends State<Shop>
} }
mediaQuery ??= MediaQuery.of(context); mediaQuery ??= MediaQuery.of(context);
screenWidth ??= mediaQuery.size.width; screenWidth ??= mediaQuery!.size.width;
screenHeight ??= mediaQuery.size.height; screenHeight ??= mediaQuery!.size.height;
statusBarHeight ??= mediaQuery.padding.top; statusBarHeight ??= mediaQuery!.padding.top;
_sliverAppBarMaxHeight ??= screenHeight - 150.0; _sliverAppBarMaxHeight ??= screenHeight! - 150.0;
_pageScrollController ??= _shopCoordinator _pageScrollController ??= _shopCoordinator
.pageScrollController(_sliverAppBarMaxHeight - _sliverAppBarInitHeight); .pageScrollController(_sliverAppBarMaxHeight! - _sliverAppBarInitHeight);
_shopCoordinator.pinnedHeaderSliverHeightBuilder ??= () { _shopCoordinator.pinnedHeaderSliverHeightBuilder ??= () {
return statusBarHeight + kToolbarHeight + _tabBarHeight; return statusBarHeight! + kToolbarHeight + _tabBarHeight;
}; };
refresh = false; refresh = false;
@@ -513,8 +513,8 @@ class ShopState extends State<Shop>
Positioned( Positioned(
top: 0, top: 0,
right: 0, right: 0,
left: screenWidth - 100, left: screenWidth! - 100,
bottom: screenHeight - 100, bottom: screenHeight! - 100,
child: Image.asset( child: Image.asset(
'assets/images/under_renovation.png', 'assets/images/under_renovation.png',
width: 200.0, width: 200.0,
@@ -953,7 +953,7 @@ class ShopState extends State<Shop>
color: Colors.white, color: Colors.white,
)), )),
Container( Container(
width: mediaQuery.size.width - 30.0, width: mediaQuery!.size.width - 30.0,
padding: EdgeInsets.only(right: 10.0, bottom: 10.0), padding: EdgeInsets.only(right: 10.0, bottom: 10.0),
child: new Text( child: new Text(
_business!.bulletin!.isEmpty ? '' : _business!.bulletin!, _business!.bulletin!.isEmpty ? '' : _business!.bulletin!,