简体   繁体   English

当我在 flutter 2.2.2 中使用地图时出现错误。(无法将参数类型“对象?”分配给参数类型“字符串”。)

[英]error shows up when i use maps in flutter 2.2.2 .(The argument type 'Object?' can't be assigned to the parameter type 'String'.)

I was going with a course in Udemy , the instructer was using flutter 1.5.4 , i did exactly what he did but i end up with an error , the instructer got his error removed as he added ['questionText']我正在 Udemy 上一门课程,讲师使用的是 flutter 1.5.4,我完全按照他的做法做了,但最终出现错误,讲师在添加 ['questionText'] 时删除了他的错误

 Widget build(BuildContext context) {
   var questions = [
     {
       'questionText': 'What\'s your favorite color from these?',
       'answers ': ['Red', 'Blue', 'Green', 'Yellow'],
     },
     {
       'questionText': 'What\'s your favorite animal from these?',
       'answers ': ['Dogs', 'Cats', 'Birds', 'Rabbits'],
     },
     {
       'questionText': 'What\'s your favorite gaming device?',
       'answers ': ['Pc', 'Ps', 'Xbox', 'Nintendo Switch'],
     },
   ];
   return MaterialApp(
     home: Scaffold(
       appBar: AppBar(
         backgroundColor: Colors.red.shade400,
         title: Text('main page'),
       ),
       body: Column(
         children: [
           Question(
             questions[_questionIndex]['questionText'],

**The argument type 'Object?' can't be assigned to the parameter type 'String'.**
           

),
           Answer(_answerQuestion),
           Answer(_answerQuestion),
           Answer(_answerQuestion),
         ],
       ),
     ),
   );
 }
}

You need to do like this你需要这样做

Question((questions[_questionIndex]['questionText'] as String?) ?? ''),

If your are sure about your value will be String from map then you can use如果您确定您的值将是来自地图的字符串,那么您可以使用

Question((questions[_questionIndex]['questionText'] as String)),

If you are following a teacher, better use the same version of Flutter SDK he is using.如果您正在跟随老师,最好使用他正在使用的相同版本的 Flutter SDK。 A lot has changed between these versions.这些版本之间发生了很多变化。 Null safety is one.零安全性就是其中之一。 If you plan to follow this course, use the below quick fix for every dart file you make.如果您打算学习本课程,请对您制作的每个 dart 文件使用以下快速修复程序。 Add:添加:

// @dart=2.9

at the top of your dart files to disable null safety.在 dart 文件的顶部禁用空安全。

I definitely think you should look for an updated course, as null safety is one of the important concepts in Dart/Flutter.我绝对认为您应该寻找更新的课程,因为空安全是 Dart/Flutter 中的重要概念之一。 And you should train with null safety enabled.并且您应该在启用空安全的情况下进行训练。

For that matter, try this:就此而言,试试这个:

questions[_questionIndex]['questionText']?.toString() ?? ''

The logic is this:逻辑是这样的:

variable?.toString() ??变量?.toString() ?? '' means dart will check if variable exists and is not null. '' 表示 dart 将检查变量是否存在且不为空。 If it is not, then the expression on the left side of ??如果不是,那么左边的表达式?? is performed: here it returns the desired value as String type.执行:这里它返回所需的值作为字符串类型。 If it was null, the expression on the right will be performed;如果为null,则执行右边的表达式; here it will return an empty string '' .在这里它将返回一个空字符串''

Without null safety, there could be a situation where questions[_questionIndex]['questionText'] is not present or created (hence null).如果没有空安全,可能会出现问题 [_questionIndex]['questionText'] 不存在或未创建(因此为空)的情况。 If the code were to be run in this situation, it would crash.如果在这种情况下运行代码,它就会崩溃。

暂无
暂无

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

相关问题 参数类型“字符串?” 不能分配给参数类型“字符串”。 当我返回文件 object 时出现此错误 - The argument type 'String?' can't be assigned to the parameter type 'String'. getting this error when i am returning File object 参数类型“String”不能分配给参数类型“Uri”。 Flutter - The argument type 'String' can't be assigned to the parameter type 'Uri'. Flutter Flutter 错误:无法将参数类型“Color Function(String)”分配给参数类型“dynamic Function(dynamic)” - Flutter error: The argument type 'Color Function(String)' can't be assigned to the parameter type 'dynamic Function(dynamic)' 我收到错误参数类型“RemoteMessage”无法分配给参数类型“Map”<String, dynamic> &#39; - I'm getting error The argument type 'RemoteMessage' can't be assigned to the parameter type 'Map<String, dynamic>' Flutter - 参数类型 &#39;Complexity/*1*/&#39; 不能分配给参数类型 &#39;Complexity/*2*/ - Flutter - argument type 'Complexity/*1*/' can't be assigned to the parameter type 'Complexity/*2*/ 参数类型 SearchBar 不能分配给 flutter 中的参数类型 Widget - The argument type SearchBar can't be assigned to the parameter type Widget in flutter Flutter - 在自动完成搜索位置 - 参数类型 &#39;List<String> ?&#39; 不能分配给参数类型“列表”<String> &#39; - Flutter - In autocomplete search location - The argument type 'List<String>?' can't be assigned to the parameter type 'List<String>' 参数类型 &#39;List<string> &#39; 不能分配给参数类型 &#39;String&#39; - The argument type 'List<string>' can't be assigned to the parameter type 'String' 参数类型'响应<dynamic> ' 不能分配给 Flutter 上的参数类型 'String'</dynamic> - The argument type 'Response<dynamic>' can't be assigned to the parameter type 'String' on Flutter 参数类型“对象?” 不能分配给参数类型“int” - The argument type 'Object?' can't be assigned to the parameter type 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM