Vaibav79
Vaibav79

Reputation: 257

The method isn't defined for the class - Flutter

I'm new to flutter and have been trying to make a simple quiz app. I've encountered an error and I'm not sure what is wrong.

Error:

Compiler message:                                                       
lib/main.dart:37:17: Error: The method 'Answer' isn't defined for the class '_MyAppState'.
 - '_MyAppState' is from 'package:project2/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'Answer'.
                Answer(),                                               
                ^^^^^^                                                  
lib/main.dart:38:17: Error: The method 'Answer' isn't defined for the class '_MyAppState'.
 - '_MyAppState' is from 'package:project2/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'Answer'.
                Answer(),                                               
                ^^^^^^                                                  
lib/main.dart:39:17: Error: The method 'Answer' isn't defined for the class '_MyAppState'.
 - '_MyAppState' is from 'package:project2/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'Answer'.
                Answer()                                                
                ^^^^^^     

main.dart:

import 'package:flutter/material.dart';
import './questions.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;

  var _questions = ["Question 1?", "Question 2?", "Question 3?"];

  void _answerQuestion() {
    setState(() {
      _questionIndex = _questionIndex + 1;
    });

    print("You answered the question!");
  }

  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text("Quiz"),
            ),
            body: Column(
              children: <Widget>[
                Question(_questions[_questionIndex]),
                Answer(),
                Answer(),
                Answer()
              ],
            )));
  }
}

answer.dart:

import 'package:flutter/material.dart';

class Answer extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Container(
        width: double.infinity,
        color: Colors.blue,
        child: RaisedButton(child: Text("Answer 1"), onPressed: null));
  }
}

I have used the same class name and imported the right file into main.dart. I'm not sure what's wrong. Can someone please point it out to me. Thanks in advance!

Upvotes: 18

Views: 109586

Answers (9)

Basit Malik
Basit Malik

Reputation: 1

I encountered this Error The context was "You are not defining a 'screen'" even i had imported the Screen correctly. I solved this Error by calling the method of Screen by the same name of class Stateless Widget that I created. The Error Would vanish if you call method by the same "name" of class "name". And obviously do check import of the screen.

Upvotes: 0

VIVIN MEVADA
VIVIN MEVADA

Reputation: 1

If inbuilt method of dart is giving this error, then you should try

import 'dart:io' as io; 

import this method and then try like this

io.method_name(...)

Upvotes: 0

Kirill_code
Kirill_code

Reputation: 139

For me helped

flutter upgrade --force

command

Upvotes: 1

Ram Prasanna
Ram Prasanna

Reputation: 11

Same Problem...

I fixed it by removing .pub-cache/ folder and ran flutter clean

Upvotes: 1

Baskar PC
Baskar PC

Reputation: 189

I tried all the solutions given above and nothing worked for me.This is the problem with the Flutter SDK and the Dart versions. Finally upgraded the flutter sdk and that did the trick. This was suggested here GitHub solution

flutter upgrade --force

Upvotes: 4

sayana dinesh
sayana dinesh

Reputation: 119

I to had a similar issue. Even though my import statement is correct i had this issue.

Then i have did "flutter clean" and after "flutter run" it worked.

So, just clean the build using "flutter clean" and see. hope it works.

Upvotes: 10

Asmoun
Asmoun

Reputation: 1747

I don't recommend writing imports by your self, when you have some widget or function that is not imported an error will be highlighted with indicating suggestions to fix that . here is an example :

  • call your function or widget, here I have a widget to call :
    calling a widget named : ScreenLogin()

  • as you can see an error is highlighted suggesting to import

  • click that then the widget is import correctly
    imported widget

this will prevent such errors for you in the future

Upvotes: 10

John Doh
John Doh

Reputation: 29

Not same but similar error. I did a hot restart instead of a hot reload. note: Windows 10, Android Studio IDE

Upvotes: 1

Omatt
Omatt

Reputation: 10453

I'm unable to replicate the same error that you received without a complete minimal repro and steps to reproduce the behavior. In my testing, an error occurred when import 'answer.dart'; was manually added in main.dart and ran the app with hot reload. Since it was a hot reload and the import was manually added, it's likely that code changes weren't included, causing the issue. The app works fine after a restart, and the issue doesn't occur when you let the IDE auto-complete the import. 

With the current details, I'm unable to rule out any definite cause for your issue.

I'm running on Flutter stable channel version 1.22.0

Error I got:

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following _CompileTimeError was thrown building MyApp(dirty, state: _MyAppState#2340f):
Unimplemented handling of missing static target

The relevant error-causing widget was: 
  MyApp file:///Users/{USER}/Downloads/dev/flutter/sample60384439/lib/main.dart:5:23
When the exception was thrown, this was the stack: 
#0      StatefulElement.build (package:flutter/src/widgets/framework.dart:4744:28)
#1      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4627:15)
#2      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11)
#3      Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
#4      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2730:33)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
Reloaded 2 of 530 libraries in 220ms.

Upvotes: 1

Related Questions