Flutter 元素类型';问题';can';不能分配给列表类型';小部件';
我不能使用我自己的Flutter 元素类型';问题';can';不能分配给列表类型';小部件';,flutter,dart,constructor,widget,Flutter,Dart,Constructor,Widget,我不能使用我自己的类/小部件问题()从问题.dart到主.dart(主体后第三行的错误位置): 导入“包装:颤振/材料.省道”; 导入“/question.dart”; void main()=>runApp(MyApp()); 类MyApp扩展了StatefulWidget{ @凌驾 状态createState(){ //TODO:实现createState 返回_MyAppState(); } } 类MyAppState扩展了状态{ var指数=0; //将数据处理到UI的函数的状态 无效(
类/小部件问题()
从问题.dart
到主.dart
(主体后第三行的错误位置):
导入“包装:颤振/材料.省道”;
导入“/question.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
状态createState(){
//TODO:实现createState
返回_MyAppState();
}
}
类MyAppState扩展了状态{
var指数=0;
//将数据处理到UI的函数的状态
无效(回答问题){
设置状态(){
questionIndex=questionIndex+1;
});
打印(问题索引);
}
void(u pangBawas){
设置状态(){
questionIndex=questionIndex-1;
});
打印(问题索引);
}
//结束状态
//该部分负责构建用户界面
@凌驾
小部件构建(构建上下文){
变量问题=[
“你最喜欢什么颜色?”,
“你最喜欢的动物是什么?”,
“你最喜欢的食物是什么?”,
“你最喜欢什么颜色?”,
“你最喜欢的衣服是什么?”,
“你最喜欢的职位是什么?”,
];
//用户界面的开始
返回材料PP(
家:脚手架(
appBar:appBar(
标题:文本(“我的第一个应用程序”),
),
正文:专栏(
儿童:[
问题(//错误:无法将元素类型'Question'分配给列表类型'Widget'。
问题[问题索引],
),
升起的按钮(
儿童:文本(“答案1”),
记者:回答问题,
),
升起的按钮(
儿童:文本(“答案2”),
按下:()=>\u pangBawas,
),
升起的按钮(
儿童:文本(“答案3”),
已按下:(){
//....
打印(“Hi”);
}),
],
),
),
);
//用户界面结束
}
}
这里没有错误,我只在我的main.dart
文件中使用Question()
得到错误
import'包装:/flatter/material.dart';
类问题扩展了无状态小部件{
最后的字符串文本;
问题(本问题文本);
@凌驾
小部件构建(构建上下文){
返回容器(
子:文本(
问题文本,
),
);
}
}
您可以添加行程操作符来检查从问题类返回的小部件是否为空。这是因为列的子窗口小部件不能为null
body: Column(
children: [
Question(questions[questionIndex]) == null
? SizedBox()
: Question(questions[questionIndex]),
这样,如果问题类返回的小部件为空,则列将创建一个大小框,而不是容器+文本。使用插值解决一些错误,并在UI中向您显示错误。下面的代码工作正常:
import 'package:flutter/material.dart';
class Question extends StatelessWidget {
final String questiontext;
Question(this.questiontext);
@override
Widget build(BuildContext context) {
return Text("$questiontext");
}
}
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
var questionIndex = 0;
// State of function that process data to UI
void _answerQuestion() {
setState(() {
questionIndex = questionIndex + 1;
});
print(questionIndex);
}
void _pangBawas() {
setState(() {
questionIndex = questionIndex - 1;
});
print(questionIndex);
}
// End of State
// This section is responsible for builing UI
@override
Widget build(BuildContext context) {
var questions = [
'What\'s your favorite color?',
'What\'s your favorite animal?',
'What\'s your favorite food?',
'What\'s your favorite color?',
'What\'s your favorite dress?',
'What\'s your favorite position?',
];
// Start of UI
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My First App'),
),
body: Column(
children: [
Question(
//ERROR: The element type 'Question' can't be assigned to the list type 'Widget'.
"${questions[questionIndex]}",
),
RaisedButton(
child: Text('Answer 1'),
onPressed: _answerQuestion,
),
RaisedButton(
child: Text('Answer 2'),
onPressed: () => _pangBawas,
),
RaisedButton(
child: Text('Answer 3'),
onPressed: () {
//....
print('Hi');
}),
],
),
),
);
// End of UI
}
}
导入“包装:颤振/材料.省道”;
类问题扩展了无状态小部件{
最后的字符串文本;
问题(本问题文本);
@凌驾
小部件构建(构建上下文){
返回文本($questiontext);
}
}
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
状态createState(){
返回_MyAppState();
}
}
类MyAppState扩展了状态{
var指数=0;
//将数据处理到UI的函数的状态
无效(回答问题){
设置状态(){
questionIndex=questionIndex+1;
});
打印(问题索引);
}
void(u pangBawas){
设置状态(){
questionIndex=questionIndex-1;
});
打印(问题索引);
}
//结束状态
//该部分负责构建用户界面
@凌驾
小部件构建(构建上下文){
变量问题=[
“你最喜欢什么颜色?”,
“你最喜欢的动物是什么?”,
“你最喜欢的食物是什么?”,
“你最喜欢什么颜色?”,
“你最喜欢的衣服是什么?”,
“你最喜欢的职位是什么?”,
];
//用户界面的开始
返回材料PP(
家:脚手架(
appBar:appBar(
标题:文本(“我的第一个应用程序”),
),
正文:专栏(
儿童:[
问题:(
//错误:无法将元素类型“Question”分配给列表类型“Widget”。
“${问题[questionIndex]}”,
),
升起的按钮(
儿童:文本(“答案1”),
记者:回答问题,
),
升起的按钮(
儿童:文本(“答案2”),
按下:()=>\u pangBawas,
),
升起的按钮(
儿童:文本(“答案3”),
已按下:(){
//....
打印(“Hi”);
}),
],
),
),
);
//用户界面结束
}
}