18

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';
  ...
Mark
  • 425
  • 1
  • 4
  • 19
  • @Morez Tried this. Unfortunately this did not work but changed the error to "Only static members can be accessed by initializers" – Mark Apr 02 '20 at 20:11

2 Answers2

14

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();
          ...
Sisir
  • 4,584
  • 4
  • 26
  • 37
  • When I tried both of these they did not work but changed the error to "Only static members can be accessed by initializers" – Mark Apr 02 '20 at 20:40
  • 1
    Move the code inside your build method. The error will go off – Sisir Apr 02 '20 at 20:55
  • 1
    Moving the code inside of the build method and passing the variables to the functions in the above class worked. Thank you! – Mark Apr 03 '20 at 14:56
  • 5
    @Sisir "Please read the basics of any Object Oriented Programming language e.g. Dart, java, C++" -- really? Did you have to say that? – pedroremedios Jan 30 '21 at 22:41
  • 5
    Ha, thinking the same thing! Anything amounting to "Please read the manual" could be appended to any response and render this entire site useless. More helpful responses point to documentation that helps. As a new developer I'll often spend a lot of time looking around for documentation and not being successful because a) I don't know how to articulate my problem (search appropriately), b) be aware which of all the billion resources out there is useful, and/or c) the manuals are too technical in nature or expect another base layer of fundamental knowledge I don't have yet. – Corey Stewart Mar 11 '21 at 15:58
  • 1
    All that being said, your actual explanation was useful in my search. Thanks, Sisir. – Corey Stewart Mar 11 '21 at 16:00
0

This error occurs if you use non-static variable like static. Let's take a look at this example:

class MyPage extends StatefulWidget {
  final foo = Foo();
  // ...
}

class _MyPageState extends State<MyPage> {
  final newFoo = MyPage.foo; // Error
  // ...
}

MyPage.foo isn't a static member but you are using if it was.


To solve this issue, you can either make the variable static

static final foo = Foo();

or

Use widget variable to get hold of the underlying variable.

class _MyPageState extends State<MyPage> {
  @override
  void initState() {
    super.initState();
    final newFoo = widget.foo; // No Error
  }
  // ...
}
iDecode
  • 22,623
  • 19
  • 99
  • 186