【问题标题】:Flutter: Using an array of Stateful Widgets inside a Stateful Widget causes a 'A RenderFlex overflow'Flutter:在有状态小部件中使用一组有状态小部件会导致“A RenderFlex 溢出”
【发布时间】:2018-08-07 20:45:16
【问题描述】:

我正在学习 Flutter,在构建一个简单的应用程序时遇到了这种奇怪的行为。我创建了一个自定义的 Stateful Widget,但我根本无法在另一个 Stateful Widget 中使用它。这是预期的行为还是我在这里遗漏了什么?

错误是:

以下 NoSuchMethodError 被抛出构建 MediaQuery(MediaQueryData(大小:大小(375.0, 812.0), devicePixelRatio: 3.0, textScaleFactor: 1.0, padding: E​​dgeInsets(0.0, 0.0, 0.0, 34.0), viewInsets: EdgeInsets.zero, alwaysUse24HourFormat: false)): 在 null 上调用了方法“_debugTypesAreRight”。 接收方:空 尝试调用:_debugTypesAreRight(Instance of 'TextCard')

引发了另一个异常:_ScaffoldLayout 自定义多子元素 布局委托忘记布局以下子节点:

代码:

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Stateful Widget 02',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);


  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Testing nested Stateful Widgets'),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            new TextCard(),
            new TextCard()
          ],
        ),
      ),
    );
  }
}


class TextCard extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    _TextCardState createState() => new _TextCardState();
  }
}

class _TextCardState extends State<TextCard> {

  @override
  Widget build(BuildContext context) {
    return new Card(
        child: new Text('12312')
    );
  }
}

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您以错误的方式覆盖了createState() 中的TextCard

    替换:

    createState() {
        _TextCardState createState() => new _TextCardState();
      }
    

    与:

    @override
    _TextCardState createState(){
        return new _TextCardState();
      }
    

    【讨论】:

      猜你喜欢
      • 2019-09-23
      • 1970-01-01
      • 2018-09-01
      • 2020-02-19
      • 2019-06-10
      • 2021-05-19
      • 2020-03-18
      • 2018-12-04
      • 2021-07-12
      相关资源
      最近更新 更多