Flutter Instance member ‘{0}’ can’t be accessed using static access

拥有回忆 提交于 2020-12-30 06:18:07

问题


I am passing variables from one activity to another in flutter but getting the error "Instance member ‘latitude’ can’t be accessed using static access" I need it converted in that block so I can assign it to a static URL.

class Xsecond extends StatefulWidget {
  final double latitude;
  final double longitude;
  Xsecond(this.latitude, this.longitude, {Key key}): super(key: key);

  @override
  _Xsecond createState() => _Xsecond();
}

class _Xsecond extends State<Xsecond> {
  static String lat = Xsecond.latitude.toString(); // Error: Instance member ‘latitude’ can’t be accessed using static access
  ...

followed by

  ...
  String url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${lat},$lng&radius=$radius&type=restaurant&key=$api';
  ...

回答1:


In your code both latitude and longitude are defined as non-static i.e. are instance variables. Which means they can only be called using a class instance.

class _Xsecond extends State<Xsecond> {
      final xsecond = Xsecond();
      static String lat = xsecond.latitude.toString();
      ...

Please read the basics of any Object Oriented Programming language e.g. Dart, java, C++

However, in your context the first class is your StatefullWidget. So you can access that by the widget field of your state class.

FIX:

class _Xsecond extends State<Xsecond> {
          static String lat = widget.latitude.toString();
          ...


来源:https://stackoverflow.com/questions/60999971/flutter-instance-member-0-can-t-be-accessed-using-static-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!