簡體   English   中英

相關的導致錯誤的小部件是 Scaffold

[英]The relevant error-causing widget was 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':不是真的。 相關的導致錯誤的小部件是 Scaffold

為什么我收到這個錯誤?

錯誤鏈接

我假設錯誤來自HomeScreen小部件。 由於錯誤消息顯示lib/.../home/home.dart

我確實發現當前代碼段沒有任何問題,實際上您可以檢查此處運行的代碼。 本答案末尾提供了源代碼。

我只重命名了_NavBarState類,並且使用Containers _NavBarStatechildren小部件實例。 檢查評論以指導您。

可能使用應用程序 MainScreen 和 HomeScreen 類的完整代碼可以提供更准確的答案。

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),
      ),
    );
  }
}

暫無
暫無

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

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