我一直在尝试遵循udemy的扑动课程,在代码中它给了我这个错误:
下面是main.dart中的代码:元素类型的问题不能被分配到列表类型"部件"。
class _MyAppState extends State<MyApp> {
int _qIndex = 0;
void _ansQ() {
setState(() {
_qIndex++;
});
print('Answer Chosen');
print(_qIndex); }
@override Widget build(BuildContext context) {
var questions = [
'What's your favorite food?',
'What's your favorite color?'
];
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My First Flutter App'),
),
body: Column(
children: [
Question(questions[_qIndex]), //This is the line that gives me the error
RaisedButton(child: Text('Answer 1'), onPressed: _ansQ),
RaisedButton(
child: Text('Answer 2'),
onPressed: () => print('Answer 2 chosen')),
RaisedButton(child: Text('Answer 3'), onPressed: _ansQ)
]
)
),
); } }
下面是question.dart中的代码:
import 'package:/flutter/material.dart';
class Question extends StatelessWidget {
final String questionText;
const Question(this.questionText);
@override
Widget build(BuildContext context) {
return Text(questionText);
}
}
如果是的话,你能帮我吗?
尝试在导入后添加as qus
import 'package:'PATH'/Question.dart' as qus;
然后使用
Column(
children: [
qus.Question(questions[_qIndex]), //This is the line that gives me the error
RaisedButton(child: Text('Answer 1'), onPressed: _ansQ),
RaisedButton(
child: Text('Answer 2'),
onPressed: () => print('Answer 2 chosen')),
RaisedButton(child: Text('Answer 3'), onPressed: _ansQ)
]