m0f1us
m0f1us

Reputation: 376

Dart, error: Only static members can be accessed in initializers when use class instance

class BusInformationScreen {
  final nodeId;

  BusInformationScreen({this.nodeId}); // initialize

  GetAPI getAPI = GetAPI(nodeID: nodeId);
  var busInfoList =   getAPI
}

class GetAPI {
  final nodeID;

  GetAPI({
    this.nodeID
  });

  // Let's say this class returns a list called lst
}

class GetAPI has a parameter nodeID

I'd like to use GetAPI in BusInformationScreen class.

First, i made an instance of GetAPI which is named getAPI and i got a nodeId as a parameter of nodeID.

Second, getAPI will gives a lst and i want to save lst to busInfoList variable.

But i got Only static members can be accessed in initializers because of nodeId and getAPI.

What is wrong with my code?

Upvotes: 0

Views: 28

Answers (1)

GaboBrandX
GaboBrandX

Reputation: 2675

You should initialize GetAPI from inside your constructor:

class BusInformationScreen {
  final nodeId;
  var busInfoList;

  BusInformationScreen({this.nodeId}) {
    GetAPI getAPI = GetAPI(nodeID: nodeId);
    busInfoList = getAPI;
  }
}

Upvotes: 1

Related Questions