简体   繁体   中英

type 'String' is not a subtype of type 'Null' in emulator (flutter)

main.dart flutter

import 'package:flutter/material.dart';
import './Question.dart';
import './Answer.dart';

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

class MyApp extends StatefulWidget {

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

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


  void _answerQuestion() {

    var questions = [
      {
        'QuestionText ': 'what is your favorite sport/s team ',
        'Answer/s': ['Real Madrid', 'Barcelona', 'Chelsea', 'Liverpool'],
      },
      {
        'QuestionText ': 'what is your favorite mobile company ',
        'Answer/s': ['Samsung', 'Apple', 'Sony', 'Asus'],
      },
      {
        'QuestionText ': 'what is your favorite dish ',
        'Answer/s': ['Pasta', 'Spaghetti', 'Pizza', 'Salad'],
      },
    ];
    setState(() {
      _questionIndex = _questionIndex + 1;
    });

  }



  @override
  Widget build(BuildContext context) {
    var questions = [
      {
        'QuestionText ': 'what is your favorite sport/s team ',
        'Answer/s': ['Real Madrid', 'Barcelona', 'Chelsea', 'Liverpool'],
      },
      {
        'QuestionText ': 'what is your favorite mobile company ',
        'Answer/s': ['Samsung', 'Apple', 'Sony', 'Asus'],
      },
      {
        'QuestionText ': 'what is your favorite dish ',
        'Answer/s': ['Pasta', 'Spaghetti', 'Pizza', 'Salad'],
      },
    ];

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text("this is my first test"),
        ),
        body: Column(
          children:
          [ Question(
              questions[_questionIndex]['QuestionText'] as String),
            ...(questions[_questionIndex]['answers'] as List<String>)
                .map((answer) {
              return Answer(_answerQuestion, answer);
            }).toList()

          ],
        ),
      ),
    );
  }
}

Answers.dart

import 'package:flutter/material.dart';

class Answer extends StatelessWidget{
   final VoidCallback selectHandler;
   final String answerText;

Answer(this.selectHandler,this.answerText);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ( Container(
      width: double.infinity,

         child: ElevatedButton(
            style: ButtonStyle(
              backgroundColor:MaterialStateProperty.all(Colors.blue),
            ),
            onPressed: selectHandler ,

            child: Text(answerText),


    ),
    )
    );
  }


}

Questions.dart

import 'package:flutter/material.dart';

class Question extends StatelessWidget{
   String QuestionText;
   Question(this.QuestionText);

   @override

  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      margin: EdgeInsets.all(10),
      width: double.infinity,
        child: Text(
      QuestionText,
      style: TextStyle(fontSize: 28),
      textAlign: TextAlign.center,

        ),);
  }
  
}

the error:

    ======== Exception caught by widgets library =======================================================
The following _CastError was thrown building MyApp(dirty, state: _MyAppState#19ff3):
type 'Null' is not a subtype of type 'String' in type cast

Inside the MaterialApp you are referring to a wrong key.

questions[_questionIndex]['answers'] as List<String>

While the specified key in the map is 'Answer/s' .

Another problem could be the _answerQuestion method. You should add a check to avoid going in overflow with the index.

if(_questionIndex < 3){
   setState(() {
         _questionIndex = _questionIndex + 1;
       });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM