dart Flutter:创建MaterialColor时出现“Null check operator used on a null value”错误

q9rjltbz  于 2023-06-19  发布在  Flutter
关注(0)|答案(3)|浏览(147)

我正在创建一个自定义的MaterialColor对象,但它抛出了这个错误。

  • colors.dart*(我在这里创建MaterialColor对象)
import 'package:flutter/material.dart';

class JarsColors {
  static const MaterialColor palette = MaterialColor(
    0xFF276678,
    <int, Color> {
      0: Color(0xFFF6F5F5),
      100: Color(0xFFD3E0EA),
      400: Color(0xFF1687A7),
      500: Color(0xFF276678),
    }
  );
}
  • 主.dart*
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: JarsColors.palette,
      ),
      home: JarsHome(title: 'Hello'),
    );
  }
}

错误输出:

The following _CastError was thrown building MyApp(dirty):
Null check operator used on a null value

The relevant error-causing widget was
MyApp
When the exception was thrown, this was the stack
#0      new ThemeData
#1      MyApp.build
#2      StatelessElement.build
#3      ComponentElement.performRebuild
#4      Element.rebuild

flutter doctor输出

λ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.0.3, on Microsoft Windows [Version 10.0.19042.928], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[√] Chrome - develop for the web
[√] Android Studio (version 4.1.0)
[√] VS Code (version 1.55.2)
[√] Connected device (3 available)

• No issues found!
jm2pwxwz

jm2pwxwz1#

我设法通过在MaterialColorswatch参数中添加更多阴影来使它工作:

class JarsColors {
  static const MaterialColor palette = MaterialColor(
    0xFF276678,
    <int, Color> {
      50: Color(0xFFF6F5F5),
      100: Color(0xFFD3E0EA),
      200: Color(0xFFD3E0EA),
      300: Color(0xFFD3E0EA),
      400: Color(0xFF1687A7),
      500: Color(0xFF276678),
      600: Color(0xFFD3E0EA),
      700: Color(0xFF1687A7),
    }
  );
}

在以下机构的帮助下:https://api.flutter.dev/flutter/material/MaterialColor-class.html
但是我找不到任何解释。很高兴知道。

vltsax25

vltsax252#

为了使答案简短而直接,在定义MaterialColor时,必须从50开始,然后是100,并以100到800的间隔递增,这足以让您使用它而不会出现空检查错误,请检查他们是如何做到这一点的
How to define and use custom MaterialColor in flutter

bprjcwpo

bprjcwpo3#

根据the documentation of MaterialColor class
定义一种颜色以及具有种颜色阴影的色样。
颜色的阴影是指索引。指数越大,颜色越深。有 10个有效索引:50,100,200,...,900。此颜色的值应与index 500和shade500的值相同。
因此,只有在满足以下两个条件时,才会提示此错误:
1.单子上应该有十项。
1.这些项目的索引应为50、100、200、……、900。上述两点中的任何一点缺失都会导致上述错误。我们可以填充一些虚拟颜色,以便列出10种颜色。
因此,我们可以更正您的colors.dart文件如下:

class JarsColors {
  static const MaterialColor palette = MaterialColor(
    0xFF276678,
    <int, Color> {
      0: Color(0xFFF6F5F5),
      100: Color(0xFFD3E0EA),
      200: Color(0x235C6C),
      300: Color(0xFF6894A1),
      400: Color(0xFF1687A7),
      500: Color(0xFF276678),
      600: Color(0xFFFFFFFF), //white (no relation with specified shade)
      700: Color(0xFF000000), //black (no relation with specified shade)
      800: Color(0xFFA1A1A1), //random (no relation with specified shade)
      900: Color(0xFF2A2A2A), //random (no relation with specified shade)
    }
  );
}

相关问题