from sympy import nsolve
这是我的浮动代码,当我的 _questionIndex 变量小于列表问题的长度时,我必须返回一个小部件,而我必须返回另一个小部件小部件。我已经据此编写了代码。
但是这里我的问题是,即使三元运算符中的条件为假,它也不会执行import 'package:flutter/material.dart';
import './question.dart';
import './answer.dart';
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;
final List<Map<String, dynamic>> questions = [
{
'questionText': 'What\'s your favorite color?',
'answers': ['black', 'Red', 'Green', 'white']
},
{
'questionText': 'What\'s your favorite animal?',
'answers': ['Lion', 'Tiger', 'rabbit', 'horse']
},
{
'questionText': "What\'s your favourite movie",
'answers': ['bahubali', 'frozen', 'piratesofcarbien', 'harrypotter']
},
];
bool t = true;
void _answerQuestion() {
setState(() {
_questionIndex = (_questionIndex + 1);
print("The value of t is =========${t}");
});
}
@override
Widget build(BuildContext context) {
print(questions[_questionIndex]['questionText']);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My First App'),
),
body: _questionIndex < questions.length ? Column(
children: [
Question(questions[_questionIndex]['questionText']),
...(questions[_questionIndex]['answers'] as List<String>)
.map((answer) {
return Answer(_answerQuestion, answer);
}).toList()
],
)
: Center(child: Text("You did it! "),),
),
);
}
}
答案 0 :(得分:2)
您的三元代码实际上是完全正确的。但是,一旦_questionIndex变量等于问题列表中的元素数,它将引发错误,因为它无法在索引_questionIndex处获取问题。由于遇到错误,因此永远无法通过三元操作使用else重建窗口小部件树。
我认为您实际上只需要删除这一行:
print(questions[_questionIndex]['questionText']);
希望这会有所帮助!