简体   繁体   English

如何解决“参数类型‘对象?’ 不能分配给参数类型 'String'.dart(argument_type_not_assignable)”?

[英]How can I resolve "The argument type 'Object?' can't be assigned to the parameter type 'String'.dart(argument_type_not_assignable)"?

I am trying to access the values of questionText from questions.我正在尝试从问题中访问 questionText 的值。 When I am trying to extract String values from a map, the following error is displayed on Flutter. (The code is written in Dart):当我试图从 map 中提取字符串值时,在 Flutter 上显示以下错误。(代码是用 Dart 编写的):

The argument type 'Object?'参数类型“对象?” can't be assigned to the parameter type 'String'.dart(argument_type_not_assignable)不能分配给参数类型 'String'.dart(argument_type_not_assignable)

Error :错误

在此处输入图像描述

This is my main.dart file:这是我的main.dart文件:

import 'package:flutter/material.dart';

import './question.dart';

import './answer.dart';

// void main() {
//   runApp(MyApp());
// }

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  var _questionIndex = 0;

  void _answerQuestion() {
    setState(() {
      _questionIndex += 1;
    });
    print(_questionIndex);
  }

  @override
  Widget build(BuildContext context) {
    var questions = [
      {
        'questionText': 'What\'s your favorite color?',
        'answers': ['Black', 'Red', 'Green', 'White'],
      },
      {
        'questionText': 'What\'s your favorite animal?',
        'answers': ['Rabbit', 'Snake', 'Elephant', 'Lion'],
      },
      {
        'questionText': 'Who\'s your favorite instructor?',
        'answers': ['Max', 'Max', 'Max', 'Max'],
      },
    ];
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('My First App'),
        ),
        body: Column(
          children: [

Error on this line:此行错误:

    Question(
                  questions[_questionIndex]['questionText'],
                ),
                Answer(_answerQuestion),
                Answer(_answerQuestion),
                Answer(_answerQuestion),
              ],
            ),
          ),
        );
      }
    }

This is my question.dart file:这是我的question.dart文件:

    import 'package:flutter/material.dart';
    
    class Question extends StatelessWidget {
      final String questionText;
    
      Question(this.questionText);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          width: double.infinity,
          margin: EdgeInsets.all(10),
          child: Text(
            questionText,
            style: TextStyle(fontSize: 28),
            textAlign: TextAlign.center,
          ),
        );
      }
    }

You need to let dart know the type of questions[_questionIndex]['questionText']你需要让dart知道问题的类型questions[_questionIndex]['questionText']

Try this:尝试这个:

Change questions[_questionIndex]['questionText']更改questions[_questionIndex]['questionText']

to questions[_questionIndex]['questionText'] as Stringquestions[_questionIndex]['questionText'] as String

In the error line在错误行

Or You can rewrite as: questions[_questionIndex]['questionText']?? ''或者你可以重写为: questions[_questionIndex]['questionText']?? '' questions[_questionIndex]['questionText']?? ''

Replace question[_questionIndex]['questionText'] By question[_questionIndex]['questionText'] as String问题[_questionIndex]['questionText']替换为问题[_questionIndex] ['questionText'] 为字符串

Hmm, that code looks familiar.嗯,这段代码看起来很熟悉。 Is it from a flutter course by Maximilian on Udemy?它来自 Maximilian 在 Udemy 上的 flutter 课程吗?

暂无
暂无

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

相关问题 不能将参数类型“String”分配给参数类型“Route”<Object?> &#39;.dart(argument_type_not_assignable) - The argument type 'String' can't be assigned to the parameter type 'Route<Object?>'.dart(argument_type_not_assignable) 参数类型'列表<string> ' 不能分配给 Dart 上的参数类型 'String'.dart(argument_type_not_assignable)</string> - The argument type 'List<String>' can't be assigned to the parameter type 'String'.dart(argument_type_not_assignable) on the Dart 不能将参数类型“Book”分配给参数类型“Map”<String, dynamic> &#39;.dart(argument_type_not_assignable) - The argument type 'Book' can't be assigned to the parameter type 'Map<String, dynamic>'.dart(argument_type_not_assignable) 参数类型 &#39;Null&#39; 不能分配给参数类型 &#39;FirebaseUser&#39;.dart(argument_type_not_assignable) - The argument type 'Null' can't be assigned to the parameter type 'FirebaseUser'.dart(argument_type_not_assignable) 错误:[dart] 无法将参数类型“Context”分配给参数类型“BuildContext”。 [argument_type_not_assignable] - error: [dart] The argument type 'Context' can't be assigned to the parameter type 'BuildContext'. [argument_type_not_assignable] 错误:参数类型“用户?” 不能分配给参数类型“用户”。 (argument_type_not_assignable 在 [food_app] lib\auth\sign_in.dart:36) - error: The argument type 'User?' can't be assigned to the parameter type 'User'. (argument_type_not_assignable at [food_app] lib\auth\sign_in.dart:36) 如何解决“参数类型‘对象’不能分配给参数类型‘字符串’ - How can i resolve "The argument type 'Object' can't be assigned to the parameter type 'String' dart - 如何解决“无法将参数类型‘Null’分配给参数类型‘MyUser’。” - dart - How can I resolve 'The argument type 'Null' can't be assigned to the parameter type 'MyUser'.' 如何解决“参数类型&#39;字符串?&#39; 无法分配给参数类型 &#39;String&#39;&quot; - Flutter - How can I resolve "The argument type 'String?' can't be assigned to the parameter type 'String' " - Flutter 如何解决“无法将参数类型&#39;String&#39;分配给参数类型&#39;int&#39;” - Flutter - How can I resolve "The argument type 'String' can't be assigned to the parameter type 'int' " - Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM