Home > Back-end >  Errors trying to convert a list of maps into a widget
Errors trying to convert a list of maps into a widget

Time:01-03

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

void main() => runApp(MyApp());

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

class _MyAppState extends State<MyApp> {
  int _indexQuestion = 0;

  void _answerQuestion() {
    setState(() {
      _indexQuestion = _indexQuestion   1;
    });
  }

  @override
  Widget build(BuildContext context) {
    var questions = [
      {
        "questionText": "Whats your favorite color?",
        "answer": ["Blue", "Yellow", "White", "Grey"]
      },
      {
        "questionText": "Whats your favorite animal?",
        "answer": ["Snake", "Rabbit", "Lion", "Gazele"]
      },
      {
        "questionText": "Who's your favorite instructor?",
        "answer": ["Max", "Felix", "Ronaldo", "Suarez"]
      },
    ];
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Quiz App"),
          backgroundColor: Colors.blueGrey,
        ),
        body: Column(
          children: [
            Questions(questions[_indexQuestion]["answer"] as String),
            ...(questions[_indexQuestion]["answer"] as List<String>)
                .map((answers) {
              return Answer(_answerQuestion, answers);
            }).toList()
          ],
        ),
      ),
    );
  }
}

Guys am trying to convert a list of strings into a widget in flutter, in the code itself it's not showing any error, but emulator is saying this -> type 'List' is not a subtype of type 'String' in type cast. What am i doing wrong though?

I also added an image, so you guys can see.enter image description here

CodePudding user response:

Change Questions(questions[_indexQuestion]["answer"] as String), to Questions(questions[_indexQuestion]["questionText"] as String),

  • Related