【问题标题】:NoSuchMethodError: The getter 'dlongitude' was called on nullNoSuchMethodError:在 null 上调用了 getter 'dlongitude'
【发布时间】:2021-07-18 05:32:22
【问题描述】:

我是新来的颤振。我试图创建一个通用类并从其他类访问它的变量。我的应用中有这个类:

class Target {
  double dlatitude;
  double dlongitude;
  Target({this.dlatitude, this.dlongitude});
}

我在其他有状态的小部件中使用这个类来设置它的变量的值:

class _EditLocationState extends State<EditLocation> {
  final TextEditingController _search = TextEditingController();

  Target target = Target();

  @override
  void dispose() {
    _search.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Positioned(
            left: 20,
            top: 50,
            right: 20,
            child: TextField(
              controller: _search,
              decoration: InputDecoration(
                filled: true,
                fillColor: Colors.grey[400],
                hintText: "${widget.location}",
                ),
              ),
            ),
            
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          var array = _search.text.split(",");
            Target(
                dlongitude: double.parse(array[0]),
                dlatitude: double.parse(array[1]));
          }
          print(target.dlatitude); // null value is printed here.
        },
        child: const Icon(Icons.done),
        backgroundColor: Colors.green,
      ),
    );
  }
}

每当我尝试访问其他小部件中的目标变量值时,都会收到此错误:NoSuchMethodError: The getter 'dlongitude' was called on null.

class _SearchRideState extends State<SearchRide> {
  final TextEditingController _search = TextEditingController();

  Target target=Target();

  @override
  void dispose() {
    _search.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Positioned(
            left: 20,
            top: 50,
            right: 20,
            child: TextField(
              controller: _search,
              decoration: InputDecoration(
                filled: true,
                fillColor: Colors.grey[400],
                hintText: "${target.dlatitude},${target.dlongitude}"
              ),
            ),
          ),
        ],
      ),
    );
  }
}

我该怎么办?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    首先,你要参考class和tour。

    现在来回答您的问题,如果您正在使用该类并想要分配值,它将像这样完成:

    Target target = Target(dlatitude: 1.1, dlongitude: 2.0);
    

    您刚刚做了Target target = Target();,这就是创建空对象以及在那里获得空值的原因。而下面的代码意义不大。

    onPressed: () {
      var array = _search.text.split(",");
      Target(
        dlongitude: double.parse(array[0]),
        dlatitude: double.parse(array[1]),
      );
      print(target.dlatitude);
    }
    

    1.这就是为什么您的代码的解决方案如下: 删除Target target = Target();

    onPressed: () {
      var array = _search.text.split(",");
      Target target = Target(
        dlongitude: double.parse(array[0]),
        dlatitude: double.parse(array[1]),
      );
      print(target.dlatitude);
    }
    

    2.或者你可以在类中提供一个默认值:

    class Target {
      double dlatitude;
      double dlongitude;
      Target({this.dlatitude=0.0, this.dlongitude=0.0});
    }
    

    然后在你的逻辑中,它会是这样的:

    Target target = Target(); 代码保持原样。和

    onPressed: () {
      var array = _search.text.split(",");
      target.dlongitud = double.parse(array[0]);
      target.dlongitude = double.parse(array[1]);
      print(target.dlatitude);
    }
    

    【讨论】:

      【解决方案2】:

      您正在使用可选参数,这意味着如果它们没有像您在示例中那样传递给构造函数:

      Target target=Target();
      

      其属性的默认值为空,因此'error: NoSuchMethodError: The getter 'dlongitude''。

      你可以做两件事:

      1. 实际上给属性赋值为var target=Target(dlatitude:0.0, dlongitude:4.5);

      2.或者为属性定义一个默认值,使它们永远不会为空

      class Target {
        double dlatitude;
        double dlongitude;
        Target({this.dlatitude=0.0, this.dlongitude=0.0});
      }
      

      我推荐你用tour这门语言写得真好。

      【讨论】:

        猜你喜欢
        • 2018-12-03
        • 2020-06-15
        • 2020-12-26
        • 1970-01-01
        • 1970-01-01
        • 2020-08-11
        • 2020-05-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多