首页 > 解决方案 > 构建仪表板时引发了以下 _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(),
       ],
     ),

有什么我做的不对吗?

标签: flutterflutter-dependenciesflutter-widget

解决方案


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

changeStatusColor(appStore.appBarColor!);

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


推荐阅读