Flutter 错误:每个子元素都必须恰好布置一次。关于建筑布局

Tom*_*eek 27 android flutter flutter-layout flutter-bloc

我开始使用颤振。这看起来很不错也很容易,但是在错误方面没有太多资源。无论如何,经过长时间的思考、检查和即兴创作,我开始在网上搜索答案,嗯,没有。所以我想问你们所有人,也许有人能够向我解释为什么我会收到这个错误,它意味着什么以及如何摆脱它。

哦,在我深入探讨之前,我可能应该提到我正在使用 flutter_bloc。

好的,所以我有这样的代码:

class Settings extends StatelessWidget {
  final _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
          title: new Text("SomeApp",style: TextStyle(color: Colors.white)),
          automaticallyImplyLeading: false,
          backgroundColor: myBlue.shade50,

          actions: <Widget>[
            IconButton(
              icon: new Icon(FontAwesomeIcons.download,color:  Colors.white),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => DownloadView()),
                );
              },
            ),
            IconButton(
              icon: new Icon(FontAwesomeIcons.chevronCircleLeft,color:  Colors.white),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => MainWindow()),
                );
              },
            ),]
      ),
      body: Container(
        padding: EdgeInsets.symmetric(vertical: 16),
        alignment: Alignment.center,
        child: new BlocBuilder<SettingsBloc, SettingsState>(
          builder: (context, state) {
            if (state is SettingsNotLoaded) {
              return new Center(
                  child: Text(
                    'count1',
                    style: TextStyle(fontSize: 24.0,color: Colors.black),
                  )
              );
            } else if (state is SettingsLoaded) {  
              return new Center(
                  child: Text(
                    'count2',
                    style: TextStyle(fontSize: 24.0,color: Colors.black),
                  )
              );
            }
            else
              {
                return new Center(
                    child: Text(
                      'count3',
                      style: TextStyle(fontSize: 24.0,color: Colors.black),
                    )
                );
              }
          },
        ),

      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

当启动我的应用程序时,我看到 appBar 就像我想要的那样,但我没有看到正文中的任何文本(我应该看到 count1、count2 或 count3),但我收到错误:“每个孩子都必须布置好正好一次。” 参考第 5 行 - return new Scaffold(。说真的,这让我发疯。有人知道吗?

提前致谢并向大家致以最诚挚的问候!

附注。当然在 flutter_bloc 页面、https: //flutter.dev/docs/development/ui/layout/tutorial 、这里和谷歌上查找信息。

编辑 1 一些语法错误 编辑 2 添加了编辑信息

小智 32

Hi i just had the same problem i think its just a bug in flutter.Stop and then reinstall your app


小智 21

最近我有同样的错误。

在我的情况下,错误是由将两个扩展小部件相互嵌套引起的。

通过删除父Expanded Widget,错误消失了。


Pra*_* GM 20

这不是颤振的错误,我遇到了同样的问题,尽管上述解决方案是正确的,但有时似乎会失败。

试试下面的命令

flutter clean
Run Code Online (Sandbox Code Playgroud)

和

flutter pub get
Run Code Online (Sandbox Code Playgroud)

然后再次运行

flutter run
Run Code Online (Sandbox Code Playgroud)

或者

或者,如果您使用的是 Android Studio(已启用 Flutter 插件)

转到工具>颤振>颤振清洁

然后再次运行应用程序,将执行与上述命令相同的操作

编辑:在熟悉 Flutter 大约一周后,现在我明白了在代码中进行更改,例如在 pubspec.yaml 中添加新的依赖项需要停止应用程序并再次运行,我确实注意到 IDE 中的 GUI(我使用 Android Studio)可能有一些问题,命令行中的上述命令工作正常。

快乐编码:)