方法 '>' 在 null 上被调用

May*_*iXx 2 flutter flutter-layout

为什么我有这个错误,而所有变量都是初始化的。

????????? 渲染库捕获的异常????????????????????????????????? RenderBox 未布局:_RenderLayoutBuilder#657cbrelayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flutter/src/rendering/box.dart':失败的断言:第 1681 行 pos 12:'hasSize' 用户创建导致错误的小部件的祖先是 Scaffold ???????? 渲染库捕获的异常????????????????????????????????? 方法 '>' 在 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),
                    ),
                ],
              ),
            ],
          );
        },
      ),
    );
  }
}

Run Code Online (Sandbox Code Playgroud)

And*_*sky 9

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),
                      ),
                  ],
                ),
              )
            ],
          );
        },
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)