简体   繁体   English

颤振:EdgeInsetsGeometry? 不能分配给 EdgeInsetsGeometry

[英]Flutter : EdgeInsetsGeometry? can't be assigned to EdgeInsetsGeometry

I am trying to make helper constants in my project so that I don't have to type the syntax again and again.我正在尝试在我的项目中创建辅助常量,这样我就不必一次又一次地键入语法。 This is the code.这是代码。

import 'package:flutter/material.dart';

// Images

Map<String, String> images = {
  'APP_LOGO': 'lib/resources/globals/assets/images/app_logo.png'
};

// Colors

Map<String, Color> colors = {
  'PRIMARY_FONT_COLOR': const Color(0xFF000000),
  'SECONDARY_FONT_COLOR': const Color(0xFF4FAFF5),
};

// Paddings

Map<String, EdgeInsetsGeometry> paddings = {
  // ALL
  'P_1': const EdgeInsets.all(5.0),
  'P_2': const EdgeInsets.all(10.0),
  'P_3': const EdgeInsets.all(15.0),
  'P_4': const EdgeInsets.all(20.0),

  // HORIZONTAL
  'PX_1': const EdgeInsets.symmetric(vertical: 0, horizontal: 5.0),
  'PX_2': const EdgeInsets.symmetric(vertical: 0, horizontal: 10.0),
  'PX_3': const EdgeInsets.symmetric(vertical: 0, horizontal: 15.0),
  'PX_4': const EdgeInsets.symmetric(vertical: 0, horizontal: 20.0),

  // VERTICAL
  'PY_1': const EdgeInsets.symmetric(vertical: 5.0, horizontal: 0),
  'PY_2': const EdgeInsets.symmetric(vertical: 10.0, horizontal: 0),
  'PY_3': const EdgeInsets.symmetric(vertical: 15.0, horizontal: 0),
  'PY_4': const EdgeInsets.symmetric(vertical: 20.0, horizontal: 0),
};

// Text Alignment

Map<String, TextAlign> text_alignment = {
  'CENTER': TextAlign.center,
  'LEFT': TextAlign.left,
  'RIGHT': TextAlign.right,
  'JUSTIFY': TextAlign.justify,
};

Now when I try to use these constants inside a widget like following,现在,当我尝试在像下面这样的小部件中使用这些常量时,

Image.asset(global_constants.images['APP_LOGO'].toString()),
Text(
    local_strings.headings['ABOUT_BENJI'].toString(),
    style: local_constants.text_styles['ABOUT_BENJI'],
    textAlign: global_constants.text_alignment['CENTER'],
 ),

It works as expected but I don't know what happens in case of the Padding Widget.它按预期工作,但我不知道Padding Widget 会发生什么。 Use the constant for padding, padding: global_constants.paddings['PX_5'] .使用常量填充, padding: global_constants.paddings['PX_5']

The error I get Error: The argument type 'EdgeInsetsGeometry?' can't be assigned to the parameter type 'EdgeInsetsGeometry' because 'EdgeInsetsGeometry?' is nullable and 'EdgeInsetsGeometry' isn't.我得到的Error: The argument type 'EdgeInsetsGeometry?' can't be assigned to the parameter type 'EdgeInsetsGeometry' because 'EdgeInsetsGeometry?' is nullable and 'EdgeInsetsGeometry' isn't. Error: The argument type 'EdgeInsetsGeometry?' can't be assigned to the parameter type 'EdgeInsetsGeometry' because 'EdgeInsetsGeometry?' is nullable and 'EdgeInsetsGeometry' isn't.

Now I do understand the error but, how do I overcome this.现在我确实理解了错误,但是,我该如何克服这一点。 I mean all the other constants are working except the padding widget.我的意思是除了填充小部件之外,所有其他常量都在工作。 Any suggestions are welcome.欢迎任何建议。 Thanks谢谢

This happens because the [] operator returns a nullable type of your map values.发生这种情况是因为[]运算符返回地图值的可为空类型 This is by design so it can return null if the given key is not in the map.这是设计使然,因此如果给定的键不在地图中,它可以返回 null。

To fix this, you need to add a !要解决此问题,您需要添加一个! null assertion operator and cast to a non nullable type, if you are sure that the variable is non null. null 断言运算符并强制转换为不可为 null 的类型,如果您确定该变量为非 null。 So in your case,所以在你的情况下,

padding: global_constants.paddings['PX_5']!

However, to be honest I recommend against your current design pattern - you are much better off using constants instead of maps, so但是,老实说,我建议不要使用您当前的设计模式 - 最好使用常量而不是映射,所以

abstract class Paddings {
  static const P_1 = const EdgeInsets.all(5.0);
  static const P_2 = const EdgeInsets.all(10.0);
  static const P_3 = const EdgeInsets.all(15.0);
  static const P_4 = const EdgeInsets.all(20.0);
}

and so on.等等。 That way, you sidestep the nullability problem altogether.这样,您就可以完全回避可空性问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 参数类型“EdgeInsets”不能分配给参数类型“MaterialStateProperty”<edgeinsetsgeometry?> ?</edgeinsetsgeometry?> - The argument type 'EdgeInsets' can't be assigned to the parameter type 'MaterialStateProperty<EdgeInsetsGeometry?>?' EdgeInsetsGeometry Class EdgeInsets Class 在 Flutter Z35900D987289A83AF1011D18A9FZ7C3 之间的区别 - Difference between EdgeInsetsGeometry Class EdgeInsets Class in Flutter Dart? Flutter StreamProvider <list<dogmodel> &gt; 无法分配</list<dogmodel> - Flutter StreamProvider<List<DogModel>> can't be assigned Flutter BlocProvider 无法赋值给参数类型 - Flutter BlocProvider can't be assigned to the parameter type Flutter 'File' 不能分配给 'XFile' - Flutter 'File' can't be assigned to 'XFile' flutter:无法分配给参数类型“T” - flutter: can't be assigned to the parameter type 'T' 无法分配给列表类型 &#39;Widget&#39; Flutter - can't be assigned to the list type 'Widget' Flutter Argument 类型 &#39;Context&#39; 不能分配给参数类型 &#39;BuildContext&#39; - Flutter - The Argument type 'Context' can't be assigned to the parameter type 'BuildContext' - Flutter Flutter - 参数类型 &#39;Complexity/*1*/&#39; 不能分配给参数类型 &#39;Complexity/*2*/ - Flutter - argument type 'Complexity/*1*/' can't be assigned to the parameter type 'Complexity/*2*/ 参数类型“对象?” 不能分配给参数类型“字符串”。 颤动 - The argument type 'Object?' can't be assigned to the parameter type 'String'. with flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM