簡體   English   中英

無法在 Dart 的構造函數中調用 class 方法 class

[英]Unable to call class method in the constructor of a Dart class

我是 Dart 編程的新手。 在實例化超級 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;   } }

這是Dart在編譯代碼時拋出的錯誤

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

您可以使用變量而不是方法updateListWithDefaultIcons並傳遞項目的默認值。 並且建議不要創建來自父項的items變量。

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(...)
                         .... ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM