【问题标题】:How use setState() inside BottomNavigationBar item in Flutter?如何在 Flutter 的 BottomNavigationBar 项目中使用 setState()?
【发布时间】:2021-02-16 09:10:13
【问题描述】:

我想在 BottomNavigationBar 项目中使用 DropdownButton。但无法使用 setState()。我看到这个错误:- 无法在初始化程序中访问实例成员“setState”。 尝试用不同的表达式替换对实例成员的引用dart(implicit_this_reference_in_initializer)

还有这个错误:- 无法在初始化程序中访问实例成员“numDropdownVal”。 尝试用不同的表达式替换对实例成员的引用dart(implicit_this_reference_in_initializer)

这是完整的代码:

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedIndex = 0;
  String numDropdownVal = 'One';

  static const TextStyle optionStyle =
      TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static List<Widget> _widgetOptions() {
    return<Widget>[
    Container(
      child: DropdownButton<String>(
                                isExpanded: true,
                                value: numDropdownVal,
                                items: <String>[
                                  'One',
                                  'Two',
                                  'Three',
                                  'Four',
                                  'Five',
                                  'Six'
                                ].map((name) {
                                  return DropdownMenuItem<String>(
                                    value: name,
                                    // Your row here:
                                    child: Text(
                                          name,
                                          style: TextStyle(fontSize: 18),
                                        ),
                                  );
                                }).toList(),
                                onChanged: (selectedName) {
                                  setState(() {
                                    numDropdownVal = selectedName;
                                  });
                                },
                              ),
    ),
    Text(
      'Other page',
      style: optionStyle,
    ),
    Text(
      'Another page',
      style: optionStyle,
    ),
  ];
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BottomNavigationBar Sample'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business),
            label: 'Business',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.school),
            label: 'School',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}

【问题讨论】:

  • 您也可以添加您的代码吗?
  • 还有其他错误。我会换个问题。
  • 如果您可以提供一个示例的要点,我们也许可以帮助您实现目标 :-)
  • @DeepakLohmod 请你现在检查一下
  • @MarkMooibroek 请你现在检查一下

标签: flutter flutter-state flutter-bottomnavigation


【解决方案1】:

以下错误:

无法在初始化程序中访问实例成员“setState”。 尝试用不同的替换对实例成员的引用 expressiondart(implicit_this_reference_in_initializer)

还有这个错误:- 实例成员 'numDropdownVal' 不能是 在初始化程序中访问。尝试替换对 具有不同的实例成员 expressiondart(implicit_this_reference_in_initializer)

发生是因为,正如错误所说,您在初始化变量时尝试访问实例成员(尚未实例化)。

以下代码不正确:

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedIndex = 0;
  String numDropdownVal = 'One';

  static List<Widget>  _widgetOptions = <Widget>[
    Container(
      child: DropdownButton<String>(
        isExpanded: true,
        value: numDropdownVal, // You can't access the 'numDropdownVal' variable yet
                               // because it's not yet instantiated.

    ...
  ];

  ...
}

要修复它,您可以将变量更改为implicit getter

List<Widget> get _widgetOptions => <Widget>[...];

或将其更改为返回widget列表的方法:

List<Widget> _widgetOptions () {
  return <Widget>[...];
}

【讨论】:

  • 对不起,我没有展示完整的代码。现在我用完整的代码更新了这个问题。请你再检查一次。 _widgetOptions.elementAt(_selectedIndex) 下面有这一行
  • 您不能使用elementAt 方法,因为_widgetOptions 不是列表,而是方法。您可以为此使用 implicit getterList&lt;Widget&gt; get _widgetOptions =&gt; &lt;Widget&gt;[...];
猜你喜欢
  • 2023-03-05
  • 2019-04-22
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 2019-12-02
  • 2021-12-23
  • 1970-01-01
  • 2021-08-21
相关资源
最近更新 更多