Someone
Someone

Reputation: 33

Flutter Bottom Nav Bar Static issue

I'm working with Flutter and have come across an issue with a "Only Static members can be accessed in Initializers".

I want to pass a string from one class to another, it is a UID I am trying to pass. I found a method to do it, but the only thing having an issue is the bottom navigation bar var list of pages. Here is the code:

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key, this.uid}) : super(key: key);
  final String uid;
  @override
  _ProfilePage createState() => _ProfilePage(uid);
}

class _ProfilePage extends State<MyStatefulWidget> {

  final String uid;
  _ProfilePage(this.uid);
  @override
  void initState() {
     super.initState();
  }

  int i = 2;
  var pages = [
    new LearningRoom(),
    new BrandRanking(),
    new Profile(uid), //Error is here with this
    new FabricCalculator(),
    new LookBook()
  ];

Would anyone be able to provide some help on solving this issue please? I've had a look around for about an hour and m unable to find anything that helps, I've tried converting to a method etc.

Thanks, Brad

Upvotes: 0

Views: 97

Answers (2)

Augustin R
Augustin R

Reputation: 7819

You need to move pages creation in initState(). Also do not use a new variable in _ProfilePage. You can just access uid by using widget.uid :

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key, this.uid}) : super(key: key);
  final String uid;
  @override
  _ProfilePage createState() => _ProfilePage(uid);
}

class _ProfilePage extends State<MyStatefulWidget> {

  @override
  void initState() {
    int i = 2;
    var pages = [
      LearningRoom(),
      BrandRanking(),
      Profile(widget.uid),
      FabricCalculator(),
      LookBook()
    ];
    super.initState();
  }

Note : new keywords are not mandatory with Dart 2

Upvotes: 1

Sikshya Maharjan
Sikshya Maharjan

Reputation: 296

Try initializing the pages inside the initState method

class _ProfilePage extends State<MyStatefulWidget> {

  final String uid;
  var pages;
  _ProfilePage(this.uid);

  @override
  void initState() {
     super.initState();
     pages = [
      new LearningRoom(),
      new BrandRanking(),
      new Profile(uid), //Error is here with this
      new FabricCalculator(),
      new LookBook()
  ];
  }


Upvotes: 1

Related Questions