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!
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 :
as you can see an error is highlighted suggesting to import
click that then the widget is import correctly
this will prevent such errors for you in the future
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With