flutter - 构建仪表板时引发了以下 _CastError(脏,依赖项:[MediaQuery],状态:DashboardState#783ce):
问题描述
我有一个仪表板卡片小部件页面,我在其中创建了逻辑,但是在仪表板页面上使用它时出现错误:
错误:小部件库捕获的异常, 在构建仪表板时抛出了以下 _CastError
(dirty, dependencies: [MediaQuery], state: DashboardState#783ce):
Null check operator used on a null value
相关的导致错误的小部件是仪表板
lib\…\screens\Home.dart:48
When the exception was thrown, this was the stack
#0 DashboardState.build
package:onthegoapp/…/Home/Dashboard.dart:32
代码:
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
// import 'package:flutter_svg/flutter_svg.dart';
import 'package:onthegoapp/main/utils/AppWidget.dart';
import 'package:onthegoapp/main/utils/ShConstant.dart';
import 'package:onthegoapp/main/utils/ShStrings.dart';
import '../../main.dart';
class Dashboard extends StatefulWidget {
late final Size size;
@override
DashboardState createState() => DashboardState();
}
class DashboardState extends State<Dashboard> {
// int selectedPos = 1;
// late List<T5Bill> mCards;
// @override
// void initState() {
// super.initState();
// selectedPos = 1;
// mCards = getListData();
// }
@override
Widget build(BuildContext context) {
var width = MediaQuery.of(context).size.width;
changeStatusColor(appStore.appBarColor!);
return Scaffold(
backgroundColor: appStore.scaffoldBackground,
body: Container(
alignment: Alignment.topLeft,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
// TopBar(),
Padding(
padding: EdgeInsets.only(left: 20.0, top: 20),
child: text(dashboard_text_title,
textColor: appStore.textPrimaryColor,
fontFamily: fontBold,
fontSize: textSizeXLarge),
),
Expanded(
child: Container(
padding: EdgeInsets.only(left: 20.0, right: 20),
child: GridView.builder(
scrollDirection: Axis.vertical,
physics: ScrollPhysics(),
// itemCount: mCards.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 16,
mainAxisSpacing: 16),
itemBuilder: (BuildContext context, int index) {
return Container(
padding: EdgeInsets.only(left: 16, right: 16),
decoration: boxDecoration(
radius: 16,
showShadow: true,
bgColor: appStore.scaffoldBackground),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Icon(
Icons.task, size: 30,
// size: width / 13, height: width / 13,
),
SizedBox(height: 10),
text("TODO",
textColor: appStore.textPrimaryColor,
fontSize: textSizeLargeMedium,
fontFamily: fontSemibold),
text("Plan an event ahead", fontSize: textSizeMedium),
SizedBox(height: 10),
],
),
);
},
),
),
),
],
),
),
);
}
}
我如何在仪表板页面中使用它:
body: Stack(
children: <Widget>[
Dashboard(),
],
),
有什么我做的不对吗?
解决方案
当您使用 ! 空值上的运算符。我看到操作员的代码中唯一的地方是:
changeStatusColor(appStore.appBarColor!);
这可能意味着 appStore.appBarColor 为 null 并且引发异常。
推荐阅读
- javascript - 由于 javascript 获取 PHP 值,代码无法正常运行,我不知道如何比较它们
- php - 有没有办法在 symfony 5 上通过防火墙设置登录超时?
- python - Discord.py 'VoiceState' 对象没有属性 'voice_channel'
- javascript - Vue - 为什么有些图像在生产中得到散列,而另一些则没有?
- c# - 在特定页面上的 iframe 中打开 PDF 文件
- mongodb - updateMany 操作结果中的 Mongoose 增量值 NaN
- c# - 嵌套集合属性的 EF Core 总和不起作用
- php - remove_action('woocommerce_after_shop_loop_item','woocommerce_gzd_template_single_shipping_costs_info',7)不起作用
- r - 同一ID的每行数据的累计和
- solr - solr 中的Suggester 组件和carrot2 集群有什么区别?