首页 > 解决方案 > 如何在颤动中访问地图元素并将它们显示为按钮?

问题描述

我在尝试学习 Flutter 课程时遇到错误。

有效的是它显示了问题。我正在努力寻找答案。它应该从我创建的地图中选择答案。

错误说:元素类型“Iterable”不能分配给列表类型“Widget”。

这是我的 main.dart:

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

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  var _questionIndex = 0;

  // increases the state of questionIndex by 1
  void _answerQuestion() {
    setState(() {
      _questionIndex = _questionIndex + 1;
    });
    print(_questionIndex);
  }

  var questions = [
    {
      'questionText': 'Question 1',
      'answers': ['Answer 1', 'Answer 2', 'Answer 3', 'Answer 4']
    },
    {
      'questionText': 'Question 2',
      'answers': ['Answer 1', 'Answer 2', 'Answer 3', 'Answer 4']
    },
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Meine erste App'),
        ),
        body: Column(
          children: <Widget>[
            Question(
              questions[_questionIndex]['questionText'].toString(),
            ),
            questions.map((question) {
              return Answer(question);
            })
          ],
        ),
      ),
    );
  }
}

这是我的回答。飞镖:

import 'package:flutter/material.dart';

class Answer extends StatelessWidget {
  final void Function() selectHandler;

  Answer(this.selectHandler);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width / 1.2,
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          primary: Colors.red,
          onPrimary: Colors.white,
        ),
        child: Text('Antwort 1'),
        onPressed: selectHandler,
      ),
    );
  }
}

感谢帮助

所以我更多地遵循了课程。现在在 Flutter 应用程序中出现错误:类型“String”不是类型转换中“List”类型的子类型

这是我在 main.dart 中的代码

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

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  var _questionIndex = 0;

  // increases the state of questionIndex by 1
  void _answerQuestion() {
    setState(() {
      _questionIndex = _questionIndex + 1;
    });
    print(_questionIndex);
  }

  var questions = [
    {
      'questionText': 'Question 1',
      'answers': ['Answer 1', 'Answer 2', 'Answer 3', 'Answer 4']
    },
    {
      'questionText': 'Question 2',
      'answers': ['Answer 1', 'Answer 2', 'Answer 3', 'Answer 4']
    },
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Meine erste App'),
        ),
        body: Column(
          children: <Widget>[
            Question(
              questions[_questionIndex]['questionText'].toString(),
            ),
            ...(questions[_questionIndex]['questionText'] as List<String>)
                .map((answer) {
              return Answer(_answerQuestion, answer);
            }).toList()
          ],
        ),
      ),
    );
  }
}

标签: flutterdart

解决方案


    ListView.builder(
      shrinkWrap:true,
      itemCount:questions.length,
      itemBuilder:(c,i){
        return ElevatedButton(
      onPressed:(){
     //todo handle click event
      },
      child: Text(questions[i]['questionText']),
     );
    }
   )

推荐阅读