El widget que causaba errores era Scaffold

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

'paquete: flutter / src / widgets / framework.dart': afirmación fallida: línea 4345 pos 14: 'owner._debugCurrentBuildTarget == this': no ​​es cierto. El widget que causaba errores era Scaffold

¿Por qué recibo este error?

enlace al error

Respuestas

MarcosBoaventura Oct 23 2020 at 02:21

Supongo que el error proviene del HomeScreenwidget. Como muestra el mensaje de error lib/.../home/home.dart.

No encontré nada malo con el fragmento actual y, de hecho, puede verificar el código que se ejecuta aquí . El código fuente se proporciona al final de esta respuesta.

Solo cambié el nombre de la _NavBarStateclase y las childreninstancias del widget se burlan Containers. Consulta los comentarios para guiarte.

Probablemente con el código completo de las clases de la aplicación MainScreen y HomeScreen se podría proporcionar una respuesta más precisa.

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