首页 > 解决方案 > NoSuchMethodError:在 null 上调用了 getter 'dlongitude'

问题描述

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

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}"
              ),
            ),
          ),
        ],
      ),
    );
  }
}

我该怎么办?

标签: flutterdart

解决方案


首先,您必须参考classtour

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

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);
}

推荐阅读