相关的引起错误的小部件是Scaffold [英] The relevant error-causing widget was Scaffold

查看:34
本文介绍了相关的引起错误的小部件是Scaffold的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class _NavBarState extends State<NavBar> {
  int _currentIndex = 0;
  final List<Widget> _children = [
    HomeScreen(),
    SignUpScreen(),
    ForgetPassword(),
    LoginScreen(),
  ];

  void onTappedBar(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        onTap: onTappedBar,
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(
              icon: Icon(
                Icons.search,
              ),
              label: 'Search',
              backgroundColor: Colors.black),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.favorite,
              ),
              label: 'Favorites',
              backgroundColor: Colors.black),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.notifications,
              ),
              label: 'Notifications',
              backgroundColor: Colors.black),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.more,
              ),
              label: 'More',
              backgroundColor: Colors.black),
        ],
        selectedItemColor: Color(0xffFFDA3A),
      ),
    );
  }
}

'package:flutter/src/widgets/framework.dart':失败的断言:4345行pos 14:'owner._debugCurrentBuildTarget == this':不正确.相关的引起错误的小部件是脚手架

'package:flutter/src/widgets/framework.dart': Failed assertion: line 4345 pos 14: 'owner._debugCurrentBuildTarget == this': is not true. The relevant error-causing widget was Scaffold

为什么会出现此错误?

链接到错误

推荐答案

我认为错误来自 HomeScreen 小部件.错误消息显示 lib/.../home/home.dart .

I am supposing the error is coming from HomeScreen widget. As the error message shows lib/.../home/home.dart.

我确实没有发现当前代码段有什么问题,实际上您可以在此处处检查运行的代码.此答案的末尾提供了源代码.

I did find nothing wrong with the current snippet and in fact you can check the code running here. Source code is provided at the end of this answer.

我仅重命名了 _NavBarState 类,并且 children 小部件实例使用 Containers 进行了模拟.查看评论以指导您.

I only renamed the _NavBarState class and the children widget instances are mocked with Containers. Check the comments to guide you.

可能使用应用程序MainScreen和HomeScreen类的完整代码,可以提供更准确的答案.

Probably with the full code of the app MainScreen and HomeScreen classes a more accurate answer could be provided.

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: AppMainPage(),
    );
  }
}

// the old _NavBar class just renamed.
class AppMainPage extends StatefulWidget{
  @override
  _AppMainPageState createState() => _AppMainPageState();
}

// The old _NavBarState class, just renamed.
class _AppMainPageState extends State<AppMainPage> {
  int _currentIndex = 0;
  final List<Widget> _children = [
    //HomeScreen(), mocking widget 
    Container(
      color: Colors.red,
      child: Center(
        child: Text('Search body layout'),
      ),
    ),
    
    //SignUpScreen(), mocking widget
    Container(
      color: Colors.green,
      child: Center(
        child: Text('Favorites body layout'),
      ),
      
    ),
    
    //ForgetPassword(),mocking widget
    Container(
      color: Colors.blue,
      child: Center(
        child: Text('Notification body layout'),
      ),
    ),
    
     //LoginScreen(),mocking widget
    Container(
      color: Colors.amber,
      child: Center(
        child: Text('More body layout'),
      ),
    ),
    
  ];

  void onTappedBar(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        onTap: onTappedBar,
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(
              icon: Icon(
                Icons.search,
              ),
              label: 'Search',
              backgroundColor: Colors.black),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.favorite,
              ),
              label: 'Favorites',
              backgroundColor: Colors.black),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.notifications,
              ),
              label: 'Notifications',
              backgroundColor: Colors.black),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.more,
              ),
              label: 'More',
              backgroundColor: Colors.black),
        ],
        selectedItemColor: Color(0xffFFDA3A),
      ),
    );
  }
}

这篇关于相关的引起错误的小部件是Scaffold的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆