我试图通过使用颤动来制作一个简单的计算器,但是当我运行它时; 有类似的错误。我在Google上搜索了2个小时,但没有成功。
我用来制作此应用程序的教程: https://www.youtube.com/watch?v=4NhLLmrB4I4
窗口小部件库捕获到异常 ══════════════════════════════════════════════════ following以下 NoSuchMethodError引发了构建Builder:方法 在空值上调用了“ _debugTypesAreRight”。接收者:null已尝试 调用:_debugTypesAreRight(“ MyCalculator”的实例)
代码:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
/**
* Simple calculator
*/
void main() => runApp(MyApp());
class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Calculator",
home: MyCalculator()
);
}
}
class MyCalculator extends StatefulWidget{
@override
State<StatefulWidget> createState() {
Calculator();
}
}
class Calculator extends State<MyCalculator>{
//TextFormField'e gelecek A ve B sayıları için conroller
final controller_numberA = TextEditingController();
final controller_numberB = TextEditingController();
final my_form_key = GlobalKey<FormState>();
String result = "";
String textToShow = "";
//toplam fonk.
void sum(){
//Formu kontrol et
//Seçilen işlem var ise
if(my_form_key.currentState.validate()){
int numberA = int.parse(controller_numberA.text);
int numberB = int.parse(controller_numberB.text);
int result = numberA + numberB;
setState(() {
//Yapılan işlemi gösterecek olan text
textToShow="$numberA + $numberB = $result";
});
}
}
void minus(){
//Formu kontrol et
//Seçilen işlem var ise
if(my_form_key.currentState.validate()){
int numberA = int.parse(controller_numberA.text);
int numberB = int.parse(controller_numberB.text);
int result = numberA - numberB;
setState(() {
//Yapılan işlemi gösterecek olan text
textToShow="$numberA - $numberB = $result";
});
}
}
void times(){
//Formu kontrol et
//Seçilen işlem var ise
if(my_form_key.currentState.validate()){
int numberA = int.parse(controller_numberA.text);
int numberB = int.parse(controller_numberB.text);
int result = numberA * numberB;
setState(() {
//Yapılan işlemi gösterecek olan text
textToShow="$numberA * $numberB = $result";
});
}
}
void divide(){
//Formu kontrol et
//Seçilen işlem var ise
if(my_form_key.currentState.validate()){
int numberA = int.parse(controller_numberA.text);
int numberB = int.parse(controller_numberB.text);
double result = numberA / numberB;
setState(() {
//Yapılan işlemi gösterecek olan text
textToShow="$numberA / $numberB = $result";
});
}
}
@override
Widget build(BuildContext context) {
//Tasarımı yapalım
return Scaffold(
body: Form(
key: my_form_key,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//2 tane TextFieldForm oluşturalım
TextFormField(
controller: controller_numberA,
validator: (value){
if(value.isEmpty) return "Please enter a number";
},
decoration: InputDecoration(hintText: "Enter a number"),
keyboardType: TextInputType.number),
TextFormField(
controller: controller_numberB,
validator: (value){
if(value.isEmpty) return "Please enter a number";
},
decoration: InputDecoration(hintText: "Enter a number"),
keyboardType: TextInputType.number),
//Sonuç TexField'i oluşturma
Text(textToShow,style: TextStyle(fontSize: 20.0),),
//Buton dizisi oluşturalım
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: sum,
child: Text('+'),
),
RaisedButton(
onPressed: minus,
child: Text('-'),
),
RaisedButton(
onPressed: times,
child: Text('*'),
),
RaisedButton(
onPressed: divide,
child: Text('/'),
),
],
)
],
),
)
);
}
}
答案 0 :(得分:2)
再次检查您的createState,它应该为State<MyCalculator> createState
,并且应该返回Calculator(),但是您从不返回任何内容
class MyCalculator extends StatefulWidget{
@override
State<MyCalculator> createState() {
return Calculator();
}
}
答案 1 :(得分:0)
createState()
中的 MyCalculator
方法期望返回类型为State<MyCalculator>
class MyCalculator extends StatefulWidget{
@override
State<MyCalculator> createState() {
return Calculator();
}
}