[英]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 String
到questions[_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.