flutter - 在 null 上调用了方法 '>'

标签 flutter flutter-layout

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

════════ Exception caught by rendering library ═════════════════════════════════ RenderBox was not laid out: _RenderLayoutBuilder#657cb relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flutter/src/rendering/box.dart': Failed assertion: line 1681 pos 12: 'hasSize' User-created ancestor of the error-causing widget was Scaffold ════════ Exception caught by rendering library ═════════════════════════════════ The method '>' was called on null. Receiver: null Tried calling: >(1e-10) User-created ancestor of the error-causing widget was 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),
                    ),
                ],
              ),
            ],
          );
        },
      ),
    );
  }
}

最佳答案

Column 内的

GridView 会导致此错误。尝试将其包装在 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),
                      ),
                  ],
                ),
              )
            ],
          );
        },
      ),
    );
  }
}

关于flutter - 在 null 上调用了方法 '>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58559145/

相关文章:

flutter - 如何处理 Flutter 中的登录屏幕滚动?

dart - 按下一个 MaterialButton 时更改一个 MaterialButton 的颜色?

flutter - Dart 中 List<String>.from() 和 as List<String> 的区别

flutter - 如何在 flutter 中生成 Animation<T>

android - 停止按钮不适用于 Android Studio 中的 flutter

flutter - 使Draggable在取消时不返回

flutter - 在 Flutter 中获取父 widget 的 borderRadius

flutter - 如何以编程方式检测共享表在 flutter 中打开或关闭

Flutter:如何以编程方式添加 TextField 或 TextFormField

flutter - 为什么这个内存泄漏?