Home > Software design >  Facing "Each child must be laid out exactly once. The relevant error-causing widget was: Scaffo
Facing "Each child must be laid out exactly once. The relevant error-causing widget was: Scaffo

Time:09-21

cause of the scaffold the bottom navigation bar is not responding and the App UI just entered a while loop 1: it appears the circular progress I made 2: I should give this app this week 3: am new to flutter

So I'd like to ask all of you, maybe someone will be able to explain to me why I get that error, what does it mean, and how to get rid of it.

any answers for this problem ?

import 'package:MeeM/pages/notification.dart';
import 'package:MeeM/pages/user.dart';
import 'package:flutter/material.dart';
import 'package:MeeM/pages/Home.dart';
import 'package:MeeM/pages/search.dart';
import 'package:MeeM/add/add.dart';
import 'package:MeeM/profileUI/profile.dart';
import 'package:MeeM/pages/signup_page.dart';

// ignore: camel_case_types
class nav extends StatefulWidget {
  @override
  _navState createState() => _navState();
}

// ignore: camel_case_types
class _navState extends State<nav> {
  int _currentindex = 0;
  List<Widget> _widgeroption = <Widget>[
    Home(currentUser: currentUser),
    search(),
    add(currentUser: currentUser),
    notification(),
    profile(profileId: currentUser?.id),
  ];

  void _onItemTap(int index) {
    setState(() {
      _currentindex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _currentindex,
        children: _widgeroption,
      ),
      bottomNavigationBar: BottomNavigationBar(
        elevation: 0,
        type: BottomNavigationBarType.fixed,
        backgroundColor: Colors.white,
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            // ignore: deprecated_member_use
            icon: Icon(
              Icons.home_outlined,
              color: Colors.black,
              size: 30,
            ),
            label: 'home',
          ),
          BottomNavigationBarItem(
            // ignore: deprecated_member_use
            icon: Icon(
              Icons.search,
              color: Colors.black,
              size: 30,
            ),
            label: 'search',
          ),
          BottomNavigationBarItem(
            // ignore: deprecated_member_use
            icon: Icon(
              Icons.add_box_outlined,
              color: Colors.black,
              size: 30,
            ),
            label: 'upload',
          ),
          BottomNavigationBarItem(
            icon: Icon(
              Icons.notifications_none_outlined,
              color: Colors.black,
              size: 30,
            ),
            label: 'feed',
          ),
          BottomNavigationBarItem(
            // ignore: deprecated_member_use
            icon: Icon(
              Icons.person,
              color: Colors.black,
              size: 30,
            ),
            label: 'profile',
          ),
        ],
        showSelectedLabels: false,
        showUnselectedLabels: false,
        currentIndex: _currentindex,
        onTap: _onItemTap,
      ),
    );
  }
}
~~~!

CodePudding user response:

I think it can be an error in your Child components in _widgeroption. for example, check that you have not used two same widgets (like Expanded widget) as a nested widget. If not, it can be due to the caching process of Flutter. do these steps:

  1. in the command line enter flutter clean and press enter.
  2. then run flutter pub get
  3. and finally run flutter run

for more info see this.

CodePudding user response:

You have to replace this:

 body: IndexedStack(
        index: _currentindex,
        children: _widgeroption,
      ),

by this:

 body: IndexedStack(
        index: _currentindex,
        children: <Widget>[
           Home(currentUser: currentUser),
           search(),
           add(currentUser: currentUser),
           notification(),
           profile(profileId: currentUser?.id),
        ],
      ),

The widgets in children have to rebuild when you call setState but the predefined _widgeroption can't rebuild because it's outside the build method.

You can also define the _widgeroption inside the build method to make it works:

[...]
 @override
  Widget build(BuildContext context) {
    List<Widget> _widgeroption = <Widget>[
      Home(currentUser: currentUser),
      search(),
      add(currentUser: currentUser),
      notification(),
      profile(profileId: currentUser?.id),
    ];
    return Scaffold(
[...]
  • Related