方法'>'在null

时间:2019-10-25 13:09:16

标签: flutter flutter-layout

为什么在所有变量都被初始化的情况下出现此错误?

  

════════渲染库捕获到异常════════   未布置RenderBox:_RenderLayoutBuilder#657cb relayoutBoundary = up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE   'package:flutter / src / rendering / box.dart':   断言失败:1681行pos 12:'hasSize'   用户创建的导致错误的小部件的祖先是       脚手架   rendering渲染库捕获到异常════════   方法'>'在null上被调用。   接收者:null   尝试致电:>(1e-10)   用户创建的导致错误的小部件的祖先是       OrientationBuilder   lib \ no_events.dart:31   ═══════════════════════

class _NoEvents extends State<NoEvents> {
  List<Contributor> contributors = [];
  List<Event> events = [];
  Contributor contributor = new Contributor(1, "XXX");

  @override
  Widget build(BuildContext context) {
    contributors.add(contributor);
    Event event1 = new Event(1, contributors, DateTime(2019 - 10 - 25), "XXX", "Test1");
    Event event2 = new Event(1, contributors, DateTime.now(), "XXX", "Test2");
    Event event3 = new Event(1, contributors, DateTime.now(), "XXX", "Test3");
    events.add(event1);
    events.add(event2);
    events.add(event3);

    return Scaffold(
      appBar: AppBar(
        title: new Text('Lille Events'),
      ),
      body: new OrientationBuilder(
        builder: (context, orientation) {
          return new Column(
            children: <Widget>[
              new GridView.count(
                crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
                children: <Widget>[
                  for (var e in events)
                    new Center(
                      child: new Text(e.subject),
                    ),
                ],
              ),
            ],
          );
        },
      ),
    );
  }
}

3 个答案:

答案 0 :(得分:2)

GridView内部的

Column导致此错误。尝试将其包装在Expanded

class _NoEvents extends State<NoEvents> {
  List<Contributor> contributors = [];
  List<Event> events = [];
  Contributor contributor = new Contributor(1, "XXX");

  @override
  Widget build(BuildContext context) {
    contributors.add(contributor);
    Event event1 =
        new Event(1, contributors, DateTime(2019 - 10 - 25), "XXX", "Test1");
    Event event2 = new Event(1, contributors, DateTime.now(), "XXX", "Test2");
    Event event3 = new Event(1, contributors, DateTime.now(), "XXX", "Test3");
    events.add(event1);
    events.add(event2);
    events.add(event3);

    return Scaffold(
      appBar: AppBar(
        title: Text('Lille Events'),
      ),
      body: OrientationBuilder(
        builder: (context, orientation) {
          return Column(
            children: <Widget>[
              Expanded(
                child: GridView.count(
                  crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
                  children: <Widget>[
                    for (var e in events)
                      Center(
                        child: Text(e.subject),
                      ),
                  ],
                ),
              )
            ],
          );
        },
      ),
    );
  }
}

答案 1 :(得分:0)

也可以在GridView下使用shrinkWrap:true

答案 2 :(得分:-1)

尝试将PageView小部件放在列中时遇到此错误。解决方法是将PageView小部件包装在Flexible()小部件中。

默认情况下,PageView小部件的大小不固定,因为它取决于其子级的大小。将其包装在Flexible()中可以解决您可能遇到的所有溢出问题。