1

我正在学习教程,现在卡住了,因为我无法调用键值。

这是我的main.dart脚本:

import 'package:flutter/material.dart';
import 'package:first_app/question.dart';
import './answer.dart';


void main() {
  runApp(MyApps());
}

class MyApps extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return MyAppsState();
  }
}

class MyAppsState extends State<MyApps> {
  int indexQuestion = 0;

  void answerQuestion() {
    setState(() {
      indexQuestion += 1;
    });
    if (indexQuestion > 1) {
      indexQuestion = 1;
    }
    print('add index by 1');
  }

  void resetIndex() {
    setState(() {
      indexQuestion = 0;
    });
    print('Reset to 0');
  }

  var a = 'as';

  Widget build(BuildContext context) {
    var questions = [
      {
        'questionText': 'What\'s your favorite animal?',
        'answers': ['cat', 'dog', 'elephant', 'fish'],
      },
      {
        'questionText': 'What\'s your favorite color?',
        'answers': ['red', 'green', 'blue', 'black'],
      },
      {
        'questionText': 'Who\'s your favorite actor?',
        'answers': ['D\'Caprio', 'Tony Hawk', 'Scarlet', 'J-Low'],
      },
    ];

    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('My First App Yo!'),
          ),
          body: Column(
            children: [
// ========================ERROR HERE========================
              Question(
                questions[indexQuestion]['questionText'],
              ),
//===========================================================
              Text('indexQuestion = $indexQuestion'),
              Answer(answerQuestion),
              Answer(answerQuestion),
              RaisedButton(
                child: Text('Answer 3'),
                onPressed: () {
                  print('Anonymus function is pressed!');
                },
              ),
              RaisedButton(
                child: Text('Answer 4'),
                onPressed: answerQuestion,
              ),
              RaisedButton(
                child: Text('Reset'),
                onPressed: resetIndex,
              ),
              Container(
                color: Colors.amber,
                width: 50,
                height: 50,
                margin: EdgeInsets.all(10),
              ),
              Center(
                child: SetGo(),
              ),
            ],
          )),
    );
  }
}

这是 question 行需要参数的question.dart文件:

    import 'package:flutter/material.dart';

class Question extends StatelessWidget {
  final String questionText;

  Question(this.questionText);

  // Question({String questionTextoption}) {
  //   questionText = questionTextoption;
  // }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text(
        'aaa $questionText',
        style: TextStyle(fontSize: 28),
        textAlign: TextAlign.center,
        maxLines: 1,
      ),
      width: double.infinity,
      margin: EdgeInsets.all(10),
    );
  }
}

错误在这一行:

Question(
                questions[indexQuestion]['questionText'],
              ),

这是错误: 参数类型“对象”不能分配给参数类型“字符串”。 在此处输入图像描述

该值将在其他文件的其他函数中作为字符串调用。有人可以帮我解决这个问题吗?

4

1 回答 1

0

您给定的代码正在运行。

在此处输入图像描述

import 'package:flutter/material.dart';
//import 'package:first_app/question.dart';
//import './answer.dart';


void main() {
  runApp(MyApps());
}

class MyApps extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return MyAppsState();
  }
}

class MyAppsState extends State<MyApps> {
  int indexQuestion = 0;

  void answerQuestion() {
    setState(() {
      indexQuestion += 1;
    });
    if (indexQuestion > 1) {
      indexQuestion = 1;
    }
    print('add index by 1');
  }

  void resetIndex() {
    setState(() {
      indexQuestion = 0;
    });
    print('Reset to 0');
  }

  var a = 'as';

  Widget build(BuildContext context) {
    var questions = [
      {
        'questionText': 'What\'s your favorite animal?',
        'answers': ['cat', 'dog', 'elephant', 'fish'],
      },
      {
        'questionText': 'What\'s your favorite color?',
        'answers': ['red', 'green', 'blue', 'black'],
      },
      {
        'questionText': 'Who\'s your favorite actor?',
        'answers': ['D\'Caprio', 'Tony Hawk', 'Scarlet', 'J-Low'],
      },
    ];

    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('My First App Yo!'),
          ),
          body: Column(
            children: [
// ========================ERROR HERE========================
              Question(
                questions[indexQuestion]['questionText'],
              ),
//===========================================================
              Text('indexQuestion = $indexQuestion'),
              //Answer(answerQuestion),
              //Answer(answerQuestion),
              RaisedButton(
                child: Text('Answer 3'),
                onPressed: () {
                  print('Anonymus function is pressed!');
                },
              ),
              RaisedButton(
                child: Text('Answer 4'),
                onPressed: answerQuestion,
              ),
              RaisedButton(
                child: Text('Reset'),
                onPressed: resetIndex,
              ),
              Container(
                color: Colors.amber,
                width: 50,
                height: 50,
                margin: EdgeInsets.all(10),
              ),
              Center(
                //child: SetGo(),
              ),
            ],
          )),
    );
  }
}



class Question extends StatelessWidget {
  final String questionText;

  Question(this.questionText);

  // Question({String questionTextoption}) {
  //   questionText = questionTextoption;
  // }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text(
        'aaa $questionText',
        style: TextStyle(fontSize: 28),
        textAlign: TextAlign.center,
        maxLines: 1,
      ),
      width: double.infinity,
      margin: EdgeInsets.all(10),
    );
  }
}
于 2020-11-02T15:29:49.887 回答