0

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
  ),

);
  }
  
}

1 Answer 1

1

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. I already fixed the typos but the problem remains. Also checked all the possible null variables (That's supposed to be the problem ) but cant be able to fix.
I ran you code and it's working fine. Did you try changing List<Map<String,Object>> to List<Map<String,dynamic>>?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.