构建仪表板时引发了以下_CastError(脏,依赖项:[MediaQuery],状态:DashboardState#783ce):

Posted

技术标签:

【中文标题】构建仪表板时引发了以下_CastError(脏,依赖项:[MediaQuery],状态:DashboardState#783ce):【英文标题】:The following _CastError was thrown building Dashboard(dirty, dependencies: [MediaQuery], state: DashboardState#783ce): 【发布时间】:2021-10-12 11:22:39 【问题描述】:

我有一个仪表板卡片小部件页面,我在其中创建了逻辑,但是在仪表板页面上使用它时出现错误:

错误: 小部件库捕获的异常, 在构建仪表板时引发了以下_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(),
       ],
     ),

有什么我做错了吗?

【问题讨论】:

【参考方案1】:

当您使用 !空值上的运算符。我在您的代码中看到操作员的唯一位置是:

changeStatusColor(appStore.appBarColor!);

这可能意味着 appStore.appBarColor 为 null 并且引发了异常。

【讨论】:

以上是关于构建仪表板时引发了以下_CastError(脏,依赖项:[MediaQuery],状态:DashboardState#783ce):的主要内容,如果未能解决你的问题,请参考以下文章

SharedPreferences.getInstance() 正在抛出 _CastError(空值检查运算符用于空值)

CastError:在路径“_id”处为值“users.client.module.js”转换为 ObjectId 失败

颤振错误,对空值使用空检查运算符

在构建 StreamBuilder<QuerySnapshot> 时引发了以下 NoSuchMethodError

CastError:模型“Company”的路径“_id”处的值“...”转换为 ObjectId 失败

CastError:模型“Company”的路径“_id”处的值“...”转换为 ObjectId 失败