简体   繁体   中英

type 'Null' is not a subtype of type 'List<map<String,Object>>' in type cast

This error keeps appearing and change from "not a subtype of type String" to what says on the screen

Error on Flutter

this is the quiz.dart

 import 'package:flutter/material.dart'; import './pregunta.dart'; import './respuesta.dart'; import './cuestionario.dart'; class Quiz extends StatelessWidget{ final List<Map<String,Object>> preguntas; final int preguntasIndex; final Function? responderPregunta; Quiz({ required this.preguntas, required this.preguntasIndex, required this.responderPregunta, }); @override Widget build(BuildContext context) { return Column( children: [ Pregunta( preguntas[preguntasIndex]['textoPregunta'] , ), ...(preguntas![preguntasIndex!]['respuestas'] as List<Map<String, Object>>) .map((respuesta) { return Respuesta(() => responderPregunta!(respuesta['puntuacion']), respuesta['texto']);}) .toList(), ], ); } }

//if here preguntas[preguntasIndex]['textoPregunta'] there is a "as String" the error in the title appears, if not the red underline appears with The argument type 'Object?' can't be assigned to the parameter type 'String'

 import 'package:flutter/material.dart'; import 'package:y_esa_senal/resultado.dart'; import './quiz.dart'; class Cuestionario extends StatefulWidget{ const Cuestionario({Key? key}) : super(key: key); @override State<StatefulWidget> createState() { return CuestionarioState(); } } class CuestionarioState extends State<Cuestionario>{ final _preguntas = [ { 'textoPreguntas': '1. Que Significa esta Señal?', 'respuestas:': [ {'texto': 'Detenerse', 'puntuacion': 1}, {'texto': 'Continuar', 'puntuacion': -1}, {'texto': 'Acelerar', 'puntuacion': -1}, {'texto': 'Mirar a ambos lados', 'puntuacion': -1}, ] }, { 'textoPreguntas': '1. Que Significa esta Señal?', 'respuestas:': [ {'texto': 'Detenerse', 'puntuacion': 1}, {'texto': 'Continuar', 'puntuacion': -1}, {'texto': 'Acelerar', 'puntuacion': -1}, {'texto': 'Mirar a ambos lados', 'puntuacion': -1}, ] }, { 'textoPreguntas': '1. Que Significa esta Señal?', 'respuestas:': [ {'texto': 'Detenerse', 'puntuacion': 1}, {'texto': 'Continuar', 'puntuacion': -1}, {'texto': 'Acelerar', 'puntuacion': -1}, {'texto': 'Mirar a ambos lados', 'puntuacion': -1}, ] }, ]; var _preguntasIndex=0; var _puntuacionTotal=0; void _reiniciarQuiz(){ setState((){ _preguntasIndex=0; _puntuacionTotal=0; }); } void _responderPregunta(int puntuacion){ _puntuacionTotal+=puntuacion; setState((){ _preguntasIndex=_preguntasIndex+1; }); print(_preguntasIndex); if(_preguntasIndex<_preguntas.length){ print('hay mas preguntas'); } else{ print('No hay mas preguntas'); } } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Geeks for Geeks'), backgroundColor: Color(0xFF00E676), ), body: Padding( padding: const EdgeInsets.all(30.0), child: _preguntasIndex < _preguntas.length ? Quiz( responderPregunta: _responderPregunta, preguntasIndex: _preguntasIndex, preguntas: _preguntas, ) //Quiz : Resultado(_puntuacionTotal, _reiniciarQuiz), ), //Padding ), //Scaffold debugShowCheckedModeBanner: false, ); //MaterialApp } }

this is the pregunta.dart

 import 'package:flutter/material.dart'; import './resultado.dart'; import './quiz.dart'; class Pregunta extends StatelessWidget{ final String textoPregunta; Pregunta(this.textoPregunta); @override Widget build(BuildContext context) { return Container( width: double.infinity, child: Text( textoPregunta!, textAlign: TextAlign.center ), ); } }

You have typos in your Cuestionario widget in the _preguntas variable.

Check both textoPreguntas and respuestas: .(extra "s" and extra ":")

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