【问题标题】:The method '>' was called on null在 null 上调用了方法“>”
【发布时间】:2020-02-21 20:14:07
【问题描述】:

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

════════ 渲染库捕获的异常═══════════════════════════════ RenderBox 未布置:_RenderLayoutBuilder#657cb relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flutter/src/rendering/box.dart': 断言失败:第 1681 行 pos 12:'hasSize' 导致错误的小部件的用户创建的祖先是 脚手架 ════════ 渲染库捕获的异常════════════════════════════════ 在 null 上调用了方法“>”。 接收方:空 尝试调用:>(1e-10) 导致错误的小部件的用户创建的祖先是 方向生成器 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),
                    ),
                ],
              ),
            ],
          );
        },
      ),
    );
  }
}

【问题讨论】:

  • 尝试将 Event() 对象创建和 List 添加到您的状态类的 initState() 方法中。
  • @Marc ``` List 贡献者 = [];列表 事件 = []; @override initState() { Contributor 贡献者 = new Contributor(1, "Evan");贡献者.add(贡献者); Event event1 = new Event(1, 贡献者, DateTime(2019 - 10 - 25), "Lille", "Test1");事件 event2 = new Event(1, 贡献者, DateTime.now(), "Lille", "Test2"); Event event3 = new Event(1, 贡献者, DateTime.now(), "Lille", "Test3");事件.add(event1);新日历(1,事件); super.initState(); } ```我一直有这个错误!

标签: flutter flutter-layout


【解决方案1】:

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),
                      ),
                  ],
                ),
              )
            ],
          );
        },
      ),
    );
  }
}

【讨论】:

    【解决方案2】:

    GridView下也可以使用shrinkWrap:true

    【讨论】:

      【解决方案3】:

      我在尝试将 PageView 小部件放入 Column 时遇到了这个错误。解决方法是将 PageView 小部件包装在 Flexible() 小部件中。

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

      【讨论】:

        猜你喜欢
        • 2020-01-17
        • 2020-08-29
        • 2021-02-11
        • 2021-01-19
        • 2019-09-12
        • 2020-10-11
        • 2020-11-06
        • 2020-07-31
        相关资源
        最近更新 更多