【问题标题】:Catch Exceptions thrown during layout捕获布局期间抛出的异常
【发布时间】:2022-01-18 13:03:27
【问题描述】:

你能捕捉到在布局 Widget 时 Flutter 中抛出的异常吗,就像在下面的示例中尝试的那样,它不起作用?

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Test',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
      body: _offendingTestBody(),
    );
  }

  Widget _offendingTestBody() {
    return SafeArea(
      child: SizedBox(
        height: 300,
        child: Container(
            color: Colors.green,
            child: _columnOrListView(List.filled(22, Text("test")))),
      ),
    );
  }

  Widget _columnOrListView(List<Widget> children) {
    try {
      return Column(
        children: children,
      );
    } catch (e) {
      return ListView(
        children: children,
      );
    }
  }
}

以下消息出现在日志中,这似乎表明渲染库 - 不必要地 - 在我的代码到达之前捕获了异常。

======== Exception caught by rendering library =====================================================
The following assertion was thrown during layout:
A RenderFlex overflowed by 74 pixels on the bottom.

The relevant error-causing widget was: 
  Column Column:file:///Users/user/StudioProjects/cols_try_catch/lib/main.dart:39:14
The overflowing RenderFlex has an orientation of Axis.vertical.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and black striped pattern. This is usually caused by the contents being too big for the RenderFlex.

Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the RenderFlex to fit within the available space instead of being sized to their natural size.
This is considered an error condition because it indicates that there is content that cannot be seen. If the content is legitimately bigger than the available space, consider clipping it with a ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex, like a ListView.

The specific RenderFlex in question is: RenderFlex#74fc7 relayoutBoundary=up4 OVERFLOWING
...  parentData: <none> (can use size)
...  constraints: BoxConstraints(0.0<=w<=390.0, h=300.0)
...  size: Size(26.0, 300.0)
...  direction: vertical
...  mainAxisAlignment: start
...  mainAxisSize: max
...  crossAxisAlignment: center
...  verticalDirection: down
◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤
====================================================================================================

【问题讨论】:

    标签: flutter


    【解决方案1】:

    我认为如果你想捕获一个错误,也许你应该将它与构建过程分开..,但有时你的过程可能会返回错误,你可以使用FutureBuilder 来捕获一个错误

    【讨论】:

    • 我正在尝试做的事情不涉及异步代码。 FutureBuilder 会有什么帮助?
    • 你好,jakob,也许你可以包装你的代码,它会抛出你的错误,并使用 FutuBuilder 来捕获和显示错误消息,你检查这个链接api.flutter.dev/flutter/widgets/FutureBuilder-class.html
    • 我熟悉doc的概念,谢谢。 ;) 获取我的代码示例并证明您的建议有效。我试过了,还是不行。
    • Widget _columnOrListViewFutureBuilder(List&lt;Widget&gt; children) { return FutureBuilder&lt;Widget&gt;( future: _columnForList(children), builder: (context, snapshot) { if (snapshot.hasError) { } else if (snapshot.hasData &amp;&amp; snapshot.data != null) { return snapshot.data!; } return ListView( children: children, ); }); return Container(); } Future&lt;Widget&gt; _columnForList(List&lt;Widget&gt; children) async { return Column( children: children, ); } 有同样的信息
    猜你喜欢
    • 1970-01-01
    • 2013-06-24
    • 2016-02-17
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 2017-10-14
    • 2011-05-07
    相关资源
    最近更新 更多