skip to Main Content

I am getting this error while debugging my flutter app , there is no error in my dart files but during debug I get this error . I am attaching my dart files here .

main.dart

import 'package:flutter/material.dart';

import 'package:quiz_app2/quiz.dart';

void main() {
  runApp(const Quiz());
}

quiz.dart

import 'package:flutter/material.dart';

import 'package:quiz_app2/firstscreen.dart';
import 'package:quiz_app2/questions.dart';

class Quiz extends StatefulWidget {
  const Quiz({key}) : super(key: key);

  @override
  State<Quiz> createState() {
    return _QuizState();
  }
}

class _QuizState extends State<Quiz> {
  Widget? activeScreen;

  @override
  void initState() {
    activeScreen = FirstScreen(switchScreen);
    super.initState();
  }

  void switchScreen() {
    setState(() {
      activeScreen = const QuestionScreen();
    });
  }

  @override
  Widget build(context) {
    return MaterialApp(
      home: Scaffold(
        body: Container(
          decoration: const BoxDecoration(
            gradient: LinearGradient(
              colors: [Colors.deepPurple, Colors.deepPurpleAccent],
            ),
          ),
          child: activeScreen,
        ),
      ),
    );
  }
}

firstScreen.dart <– The error is showing at this file while debug .

import 'package:flutter/material.dart';

class FirstScreen extends StatelessWidget {
  const FirstScreen(this.startQuiz, {key}) : super(key: key);

  final void Function() startQuiz;
  @override
  Widget build(context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Image.asset(
            'assets/images/quiz-logo.png',
            height: 400,
            color: const Color.fromARGB(150, 250, 250, 250),
          ),
          const Padding(
            padding: EdgeInsets.only(top: 40),
            child: Text(
              'Learn Flutter the fun way!',
              style: TextStyle(
                color: Color.fromARGB(255, 255, 255, 255),
                fontSize: 30,
              ),
            ),
          ),
          const SizedBox(height: 40),
          OutlinedButton.icon(
            onPressed: startQuiz,
            style: OutlinedButton.styleFrom(
              foregroundColor: Colors.white,
            ),
            icon: const Icon(Icons.arrow_right_alt),
            label: const Text(
              'Start Quiz',
              style: TextStyle(fontSize: 20),
            ),
          ),
        ],
      ),
    );
  }
}

questions.dart

import 'package:flutter/material.dart';

class QuestionScreen extends StatefulWidget {
  const QuestionScreen({key}) : super(key: key);

  @override
  State<QuestionScreen> createState() {
    return _QuestionScreenState();
  }
}

class _QuestionScreenState extends State<QuestionScreen> {
  @override
  Widget build(context) {
    return const Text('questions');
  }
}

I want the solution of this error , I have searched but not got the answer. Thanking you in anticipation.

3

Answers


  1. Chosen as BEST ANSWER

    The problem solved automatically after few reloads , now the same code is working fine without any error . Just reload again few times and check after each reload whether the error got vanished .


  2.   @override
      void initState() {
        activeScreen = FirstScreen(switchScreen);
        super.initState();
      }
    

    You do not seem to have switchScreen defined from what I can tell. So the value of this is null when in your widget definition it should be a function. That is what the error is telling you.

    Login or Signup to reply.
  3. You might need to change how you send the function or use the function.

    Send the function option:

    ...
    @override
    void initState() {
        activeScreen = FirstScreen(() => switchScreen);
        super.initState();
    }
    ...
    

    Or use the function option:

    ...
    OutlinedButton.icon(
        onPressed: () => startQuiz,
        style: OutlinedButton.styleFrom(
            foregroundColor: Colors.white,
        ),
        icon: const Icon(Icons.arrow_right_alt),
        label: const Text(
            'Start Quiz',
            style: TextStyle(fontSize: 20),
        ),
    ),
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search