flutter - 抛出另一个异常 : dependOnInheritedElement() was called before _PagesTestWidgetState. initState() 已完成

标签 flutter dart

我如何解决这个问题,这是我的代码,错误出现在 initState 和 didUpdateWidget 上,我希望有人帮助我解决这个问题,当我在抽屉上的菜单上粘贴时,我无法转到我想要导航到的页面她是我的代码,我正在使用路线在页面之间导航,所以我希望你能帮助我解决这个错误,谢谢大家

class _PagesTestWidgetState extends State<PagesTestWidget> {
  initState() {
     _selectTab(widget.currentTab);
     super.initState();
  }

    @override
   void didUpdateWidget(PagesTestWidget oldWidget) {
    _selectTab(oldWidget.currentTab);
     super.didUpdateWidget(oldWidget);
   }
    void _selectTab(int tabItem) {
setState(() {
  widget.currentTab = tabItem;
  switch (tabItem) {
    case 0:
      widget.currentTitle = S.of(context).notifications;
      widget.currentPage = NotificationsWidget();
      break;
    case 1:
      widget.currentTitle = S.of(context).profile;
      widget.currentPage = ProfileWidget();
      break;
    case 2:
      widget.currentTitle = settingsRepo.setting?.appName;
      widget.currentPage = HomeWidget();
      break;
    case 3:
      widget.currentTitle = S.of(context).my_orders;
      widget.currentPage = OrdersWidget();
      break;
    case 4:
      widget.currentTitle = S.of(context).favorites;
      widget.currentPage = FavoritesWidget();
      break;
  }
});
 }

   @override
  Widget build(BuildContext context) {
     return WillPopScope(
      onWillPop: () async => false,
      child: Scaffold(
        drawer: DrawerWidget(),
        appBar: AppBar(
          backgroundColor: Colors.transparent,
        elevation: 0,
        centerTitle: true,
      title: Text(
        widget.currentTitle ??
            settingsRepo.setting?.appName ??
            S.of(context).home,
        style: Theme.of(context)
            .textTheme
            .title
            .merge(TextStyle(letterSpacing: 1.3)),
      ),
      actions: <Widget>[
        new ShoppingCartButtonWidget(
            iconColor: Theme.of(context).hintColor,
            labelColor: Theme.of(context).accentColor),
      ],
    ),
    body: widget.currentPage,
    bottomNavigationBar: BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      selectedItemColor: Theme.of(context).accentColor,
      selectedFontSize: 0,
      unselectedFontSize: 0,
      iconSize: 22,
      elevation: 0,
      backgroundColor: Colors.transparent,
      selectedIconTheme: IconThemeData(size: 28),
      unselectedItemColor: Theme.of(context).focusColor.withOpacity(1),
      currentIndex: widget.currentTab,
      onTap: (int i) {
        this._selectTab(i);
      },
      items: [
        BottomNavigationBarItem(
          icon: Icon(Icons.notifications),
          title: new Container(height: 0.0),
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.person),
          title: new Container(height: 0.0),
        ),
        BottomNavigationBarItem(
            title: new Container(height: 5.0),
            icon: Container(
              width: 42,
              height: 42,
              decoration: BoxDecoration(
                color: Theme.of(context).accentColor,
                borderRadius: BorderRadius.all(
                  Radius.circular(50),
                ),
                boxShadow: [
                  BoxShadow(
                      color: Theme.of(context).accentColor.withOpacity(0.4),
                      blurRadius: 40,
                      offset: Offset(0, 15)),
                  BoxShadow(
                      color: Theme.of(context).accentColor.withOpacity(0.4),
                      blurRadius: 13,
                      offset: Offset(0, 3))
                ],
              ),
              child: new Icon(Icons.home,
                  color: Theme.of(context).primaryColor),
            )),
        BottomNavigationBarItem(
          icon: new Icon(Icons.fastfood),
          title: new Container(height: 0.0),
        ),
        BottomNavigationBarItem(
          icon: new Icon(Icons.favorite),
          title: new Container(height: 0.0),
        ),
      ],
    ),
  ),
);   
} 
}

最佳答案

我现在修复它,谢谢大家

 initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
        _selectTab(widget.currentTab);
    });
  }

关于flutter - 抛出另一个异常 : dependOnInheritedElement() was called before _PagesTestWidgetState. initState() 已完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61720737/

相关文章:

flutter - 创建类似于突破游戏的方 block 网格

android - E/flutter (8084): [ERROR:flutter/lib/ui/ui_dart_state. cc(157)]未处理的异常:SocketException:操作系统错误:连接被拒绝

android - Flutter UI 损坏了第一个屏幕

dart - 使用工厂方法从点创建矩形

dart - Dart VM 还在使用吗?

dart - 如何确定 Flutter 中图像的宽度和高度?

firebase - 文档引用会增加阅读次数吗?

flutter - 如何回到之前的路线?

dart - 如何使用 ScriptProcessorNode

flutter - 使用 Timer 而不是 Future.delayed(...) 创建异步等待步骤