简体   繁体   中英

Unable to call class method in the constructor of a Dart class

I am new to Dart programming. Need help in updating the List "items" before instantiating the super class.

import 'package:flutter/material.dart'; import '../util/color_constants.dart';

class CustomBottomNavigatorBar extends BottomNavigationBar{

  final List<BottomNavigationBarItem> items;

  CustomBottomNavigatorBar(
      this.items,   ): super(backgroundColor: ColorConstants.facebook_blue,items: updateListWithDefaultIcons(items));

  List<BottomNavigationBarItem> updateListWithDefaultIcons(){
    items.add(BottomNavigationBarItem(
      label: 'Settings',
      icon: Icon(Icons.settings),
      tooltip: 'Settings',
      backgroundColor: ColorConstants.white,));
    items.add(BottomNavigationBarItem(
      label: 'Profile',
      icon: Icon(Icons.man),
      tooltip: 'Profile',
      backgroundColor: ColorConstants.white,));
    return items;   } }

Here is the error that Dart throws up while compiling the code

The instance member 'updateListWithDefaultIcons' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression

You can use variable instead of method updateListWithDefaultIcons and pass default on items. and it is recommended not to create items variable that is coming from parent.

class CustomBottomNavigatorBar extends BottomNavigationBar {
  final List<BottomNavigationBarItem> items_;

  CustomBottomNavigatorBar({
    Key? key,
    this.items_ = _updateListWithDefaultIcons,
  }) : super(
          key: key,
          backgroundColor: Colors.cyanAccent,
          items: items_,
        );

  static const List<BottomNavigationBarItem> 
                    _updateListWithDefaultIcons = [
                         BottomNavigationBarItem(...)
                         .... ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM