在 null 上调用了方法“_debugTypesAreRight”

Posted

技术标签:

【中文标题】在 null 上调用了方法“_debugTypesAreRight”【英文标题】:The method '_debugTypesAreRight' was called on null 【发布时间】:2020-10-11 01:53:47 【问题描述】:

我尝试使用颤振制作一个简单的计算器,但是当我运行它时; 有错误之类的。我在 google 上搜索了 2 个小时,但没有成功。

我用来制作这个应用程序的教程: https://www.youtube.com/watch?v=4NhLLmrB4I4

小部件库捕获的异常 ══════════════════════════════════════════════════ ═════以下 NoSuchMethodError 被抛出构建生成器:方法 在 null 上调用了“_debugTypesAreRight”。接收方:空 调用:_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('/'),
                ),

              ],
            )
          ],
        ),
      )
    );
  





【问题讨论】:

【参考方案1】:

再次检查你的 createState,它应该是 State&lt;MyCalculator&gt; createState,它应该返回 Calculator(),但你永远不会返回任何东西

class MyCalculator extends StatefulWidget
  @override
  State<MyCalculator> createState() 
    return Calculator();
  


【讨论】:

在那之后你尝试过flutter clean吗?只是为了确保它运行新代码【参考方案2】:

MyCalculator 类中的createState() 方法期望返回类型为State&lt;MyCalculator&gt;

class MyCalculator extends StatefulWidget
  @override
  State<MyCalculator> createState() 
    return Calculator();
  

【讨论】:

以上是关于在 null 上调用了方法“_debugTypesAreRight”的主要内容,如果未能解决你的问题,请参考以下文章

Futtler 错误在 null 上调用了方法“toDouble”。接收方:null 尝试调用:toDouble() [重复]

在 null 上调用了方法“[]”

在 null 上调用了方法“addItem”

NoSuchMethodError:在 null 上调用了方法 '[]' - Flutter

NoSuchMethodError:在 null 上调用了方法 'ref'

Flutter 错误在 null 上调用了方法“[]”。接收方:null 尝试调用:[]("videoId")