Flutter 错误:操作员未在颤振中工作 导入“包装:颤振/材料.省道”; 导入“/question.dart”; 导入“/answer.dart”; void main()=>runApp(MyApp()); 类MyApp扩展了StatefulWidget{ @凌驾 状态createState(){ //TODO:实现createState 返回_MyAppState(); } } 类MyAppState扩展了状态{ var _指数=0; 最终问题清单=[ { “问题文本”:“你最喜欢什么颜色?”, ‘答案’:[‘黑色’、‘红色’、‘绿色’、‘白色’] }, { “问题文本”:“你最喜欢的动物是什么?”, ‘答案’:[‘狮子’、‘老虎’、‘兔子’、‘马’] }, { “问题文本”:“你最喜欢的电影是什么”, ‘答案’:['bahubali'、'Frozed'、'piratesofcarbien'、'harrypotter'] }, ]; bool t=真; 无效(回答问题){ 设置状态(){ _questionIndex=(_questionIndex+1); 打印(“t的值为====${t}”); }); } @凌驾 小部件构建(构建上下文){ 打印(问题[_问题索引]['questionText']); 返回材料PP( 家:脚手架( appBar:appBar( 标题:文本(“我的第一个应用程序”), ), 正文:_questionIndex

Flutter 错误:操作员未在颤振中工作 导入“包装:颤振/材料.省道”; 导入“/question.dart”; 导入“/answer.dart”; void main()=>runApp(MyApp()); 类MyApp扩展了StatefulWidget{ @凌驾 状态createState(){ //TODO:实现createState 返回_MyAppState(); } } 类MyAppState扩展了状态{ var _指数=0; 最终问题清单=[ { “问题文本”:“你最喜欢什么颜色?”, ‘答案’:[‘黑色’、‘红色’、‘绿色’、‘白色’] }, { “问题文本”:“你最喜欢的动物是什么?”, ‘答案’:[‘狮子’、‘老虎’、‘兔子’、‘马’] }, { “问题文本”:“你最喜欢的电影是什么”, ‘答案’:['bahubali'、'Frozed'、'piratesofcarbien'、'harrypotter'] }, ]; bool t=真; 无效(回答问题){ 设置状态(){ _questionIndex=(_questionIndex+1); 打印(“t的值为====${t}”); }); } @凌驾 小部件构建(构建上下文){ 打印(问题[_问题索引]['questionText']); 返回材料PP( 家:脚手架( appBar:appBar( 标题:文本(“我的第一个应用程序”), ), 正文:_questionIndex,flutter,Flutter,这是我的颤振代码,当我的\u questionIndex变量小于列表问题的长度时,我必须返回一个小部件,否则我必须返回另一个小部件。我已经根据这一点编写了代码。 但这里我的问题是,即使三元运算符中的条件为false,它也没有执行负的条件,即中心(child:Text(“你做到了!”),你的三元代码实际上是完全正确的。但是,一旦_questionIndex变量等于问题列表中的元素数,它就会抛出一个错误,因为它无法在索引_questionIndex处获取问题。因为它遇到了错误,所以它永远无法使用三元

这是我的颤振代码,当我的\u questionIndex变量小于列表问题的长度时,我必须返回一个小部件,否则我必须返回另一个小部件。我已经根据这一点编写了代码。
但这里我的问题是,即使三元运算符中的条件为false,它也没有执行负的条件,即
中心(child:Text(“你做到了!”),
你的三元代码实际上是完全正确的。但是,一旦_questionIndex变量等于问题列表中的元素数,它就会抛出一个错误,因为它无法在索引_questionIndex处获取问题。因为它遇到了错误,所以它永远无法使用三元操作中的else重建小部件树

我认为你实际上只需要删除这一行:

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! "),),
      ),
    );
  }
}

希望这有帮助

您的三元代码实际上是完全正确的。但是,一旦_questionIndex变量等于问题列表中的元素数,它就会抛出一个错误,因为它无法在索引_questionIndex处获取问题。因为它遇到了错误,所以它永远无法使用三元操作中的else重建小部件树

我认为你实际上只需要删除这一行:

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! "),),
      ),
    );
  }
}

希望这有帮助

非常感谢您@Alex Collete,这解决了我的错误@Chalapathinagavarmabupathira,很高兴我能帮上忙!非常感谢您@Alex Collete,这解决了我的错误@Chalapathinagavarmabupathira,很高兴我能帮上忙!