断言失败:第 320 行 pos 12:'width > 0.0':对于 Flutter 中的简单侧边栏来说不是 true

Pap*_*ika 1 dart flutter

我为我的应用程序创建了一个侧边栏来显示一些图标:

\n
Widget build(BuildContext context) {\n    return Container(\n      color: widget.backgroundColor,\n      //width: double.infinity,\n      child: Column(\n        mainAxisAlignment: MainAxisAlignment.spaceEvenly,\n        children: [\n          Container(\n              child: FittedBox(\n                  child: Image(image: AssetImage('my_icon.png')))),\n          FittedBox(child: Image(image: AssetImage('my_st.png'))),\n          FittedBox(child: Image(image: AssetImage('my_cart_2.png'))),\n          FittedBox(child: Image(image: AssetImage('my_info_2.png'))),\n          FittedBox(child: Image(image: AssetImage('my_rotate_2.png')))\n        ],\n      ),\n    );\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

尽管它呈现完美,但我收到此错误:

\n
\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90 Exception caught by rendering library \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nThe following assertion was thrown during performLayout():\n'package:flutter/src/rendering/box.dart': Failed assertion: line 320 pos 12: 'width > 0.0': is not true.\n\n\nEither the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.\nIn either case, please report this assertion by filing a bug on GitHub:\n  https://github.com/flutter/flutter/issues/new?template=BUG.md\n\nThe relevant error-causing widget was: \n  FittedBox file:///home/user/AndroidStudioProjects/flutter_app/lib/sidebar.dart:23:22\nWhen the exception was thrown, this was the stack: \n#2      BoxConstraints.constrainSizeAndAttemptToPreserveAspectRatio (package:flutter/src/rendering/box.dart:320:12)\n#3      RenderFittedBox.performLayout (package:flutter/src/rendering/proxy_box.dart:2433:30)\n#4      RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)\n#5      RenderFlex.performLayout (package:flutter/src/rendering/flex.dart:768:15)\n#6      RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)\n
Run Code Online (Sandbox Code Playgroud)\n

错误发生在第一个FittedBox. 如果我删除它,我会在第二个错误FittedBox

\n

我认为这与没有宽度有关Image,但设置width它们(只是为了测试)不起作用。

\n

即使出现错误,侧边栏也会正确呈现。

\n

Lee*_*ee3 5

无需包裹ImageFittedBox. 只需传递一个BoxFittoImagefit参数即可。

Image(image: AssetImage('my_icon.png'), fit: BoxFit.fitWidth)
Run Code Online (Sandbox Code Playgroud)