【问题标题】:Facing "Each child must be laid out exactly once. The relevant error-causing widget was: Scaffold" error in flutter面对“每个孩子必须恰好布置一次。相关的导致错误的小部件是:Scaffold”颤动中的错误
【发布时间】:2021-11-13 14:11:34
【问题描述】:

脚手架的原因底部导航栏没有响应并且App UI刚刚进入了一个while循环 1:看来我取得了循环进展 2:我这周应该给这个应用程序 3:我是 Flutter 的新手

所以我想问问大家,也许有人可以向我解释为什么我会收到这个错误,这是什么意思,以及如何摆脱它。

这个问题有什么答案吗?

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

【问题讨论】:

  • 这可能是由于相同的缓存问题,尝试停止应用程序,运行flutter clean并再次运行它

标签: android ios flutter dart


【解决方案1】:

你必须替换这个:

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

通过这个:

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

children 中的小部件必须在您调用 setState 时重建,但预定义的 _widgeroption 无法重建,因为它在 build 方法之外。

您还可以在build 方法中定义_widgeroption 以使其工作:

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

【讨论】:

    【解决方案2】:

    我认为这可能是您在 _widgeroption 中的 Child 组件中的错误。例如,检查您没有使用两个相同的小部件(如Expanded 小部件)作为嵌套小部件。 如果不是,可能是Flutter的缓存过程造成的。执行以下步骤:

    1. 在命令行中输入flutter clean并回车。
    2. 然后运行flutter pub get
    3. 最后运行flutter run

    更多信息请见this

    【讨论】:

    • 非常感谢
    • @AbderrezakDouba 很乐意为您提供帮助。如果我的回答对您有所帮助,如果您将其标记为正确的解决方案,我将不胜感激。
    猜你喜欢
    • 2020-05-21
    • 2022-07-21
    • 2021-01-30
    • 2022-01-20
    • 2021-12-29
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    相关资源
    最近更新 更多