Flutter version 1.71.1 I am getting this error "type 'Null' is not a subtype of type 'List'<String> in type cast"

Viewed 38

I've include the operator ?? and it still failed. Here is my code:

import 'package:flutter/material.dart';

import './question.dart';
import './answer.dart';

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

class MyApp extends StatefulWidget {

    const MyApp({Key? key}) : super(key: key);

    @override
    State<StatefulWidget> createState() {

    // TODO: implement createState
    return _MyAppState();
  }
}

    class _MyAppState extends State<MyApp> {

      var _questionIndex = 0;

  //_answerQuestion function passed to  widget below
      void _answerQuestion() {

    //Must use setState() function to change question on UI
      setState(() {

      _questionIndex = _questionIndex + 1;
    });
    print(_questionIndex);
  }

     @override
     Widget build(BuildContext context) {

    //List string questions with var inside of []
    var questions = [

      //This is how to develp a "MAP" = list of questions & answers
      {
        'questionText': 'Who created Fluter?',
        'answer': ['Google', 'Yahoo', 'Amtrak', 'Disney'],
      },
      {
        'questionText': 'What is your favorite class',
        'answer': ['Java', 'Mobile App', 'Dart', 'DevOps'],
      },
      {
        'questionText': 'What is your favorite Fluter feature',
        'answer': ['Class', 'Functions', 'Dart', 'Widgets'],
      },
    ];

    return MaterialApp(

      home: Scaffold(
        appBar: AppBar(

          title: Text('Flutter Quiz'),
        ),
        body: Column(

          children: [

            Question(

              (questions[_questionIndex] ['questionText']) as String,
            ),
            //... = spread operator to take a nested list and include in existing list

            **...((questions[_questionIndex] ['answers']) as List<String>)**

                .map((answer) {

              return Answer(_answerQuestion, answer);

            }).toList()
          ],
        ),
      ),
    );
  }
}
0 Answers
Related