Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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';
  ...
like image 915
Mark Avatar asked Apr 02 '20 19:04

Mark


Video Answer


1 Answers

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();
          ...
like image 58
Sisir Avatar answered Sep 20 '22 16:09

Sisir