<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Nickf2k]]></title><description><![CDATA[I'm Vuong Nguyen (nickf2k), a Flutter developer with nearly 5 YOEs. My goal is to share knowledge about Flutter and related technologies like Firebase, GenAI to]]></description><link>https://nickf2k.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1720715275493/2e659dad-b847-46dc-ba5d-cb4debdd3ec9.png</url><title>Nickf2k</title><link>https://nickf2k.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Thu, 23 Apr 2026 03:17:48 GMT</lastBuildDate><atom:link href="https://nickf2k.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Fix Common Flutter Errors: ‘RenderBox was not laid out’, ‘overflow’, and ‘Vertical viewport was given unbounded height’]]></title><description><![CDATA[June 26, 2024 9:00 AM Vương Nguyễn Thiên
1. Introduction to Common Errors

Imagine spending hours crafting a beautiful Flutter app, only to be greeted with frustrating errors like ‘RenderBox was not laid out’, ‘A RenderFlex overflowed by’, and ‘Verti...]]></description><link>https://nickf2k.dev/how-to-fix-common-flutter-errors-renderbox-was-not-laid-out-overflow-and-vertical-viewport-was-given-unbounded-height</link><guid isPermaLink="true">https://nickf2k.dev/how-to-fix-common-flutter-errors-renderbox-was-not-laid-out-overflow-and-vertical-viewport-was-given-unbounded-height</guid><category><![CDATA[Flutter error]]></category><category><![CDATA[flutter constraint]]></category><category><![CDATA[flutter expanded]]></category><category><![CDATA[flutter-flexible]]></category><category><![CDATA[Flutter]]></category><category><![CDATA[Flutter Widgets]]></category><category><![CDATA[Flutter SDK]]></category><category><![CDATA[Flutter Examples]]></category><category><![CDATA[Flutter Course ]]></category><dc:creator><![CDATA[Vương Nguyễn Thiên]]></dc:creator><pubDate>Thu, 11 Jul 2024 16:18:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1720714384945/081ffdd7-610c-4733-94c7-6e443cd0e376.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>June 26, 2024 9:00 AM <a class="user-mention" href="https://hashnode.com/@98nickf2k">Vương Nguyễn Thiên</a></p>
<h1 id="heading-1-introduction-to-common-errors">1. Introduction to Common Errors</h1>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720667656416/23a63266-f8f9-4878-b9da-967298cbefe8.png" alt class="image--center mx-auto" /></p>
<p>Imagine spending hours crafting a beautiful Flutter app, only to be greeted with frustrating errors like ‘<strong>RenderBox was not laid out</strong>’, ‘<strong>A RenderFlex overflowed by’</strong>, and ‘<strong>Vertical viewport was given unbounded height’</strong>. These common issues can make even the most seasoned developers pull their hair out. But what if I told you there’s a way to banish these errors for good? The key lies in understanding Flutter Constraints.</p>
<h1 id="heading-2-understanding-flutter-constraint">2. Understanding Flutter Constraint</h1>
<blockquote>
<p><strong>“Constraints go down, sizes go up, and the parent sets the position”</strong></p>
</blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720667938924/19cf5e26-82c1-46ca-b56e-30ad435b862d.png" alt class="image--center mx-auto" /></p>
<p>To truly understand how to handle those pesky layout errors, we need to dive into the basics of how Flutter handles constraints. The principle “constraints go down, sizes go up, and the parent sets the position” is key to this understanding.</p>
<p>• <strong>Constraints go down</strong>: Constraints are passed from the parent widget to its child widgets. The parent tells the child how much space it can take.</p>
<p>• <strong>Sizes go up</strong>: The child widget then tells the parent how much space it actually needs within those constraints.</p>
<p>• <strong>Parent sets the position</strong>: Finally, the parent widget places the child in the designated position based on the size information it receives.</p>
<p>Understanding this flow helps in diagnosing and solving layout issues. When a widget’s constraints are not set properly, errors like ‘RenderBox was not laid out’ can occur. Similarly, when a child widget’s size exceeds the parent’s constraints, you get errors like ‘A RenderFlex overflowed by’. The key to solving these errors is ensuring each widget properly communicates and respects these constraints.</p>
<h1 id="heading-3-common-errors-and-how-constraints-solve-them">3. Common Errors and How Constraints Solve Them</h1>
<p>Errors like <code>RenderBox was not laid out</code>, <code>A RenderFlex overflowed by</code>, and <code>Vertical viewport was given unbounded height</code> can be frustrating roadblocks in Flutter development. Understanding how Flutter Constraints work is the key to resolving these issues for good.</p>
<h2 id="heading-31renderbox-was-not-laid-out-error"><strong>3.1</strong><code>RenderBox was not laid out</code> Error</h2>
<p><strong>What does the error look like?</strong></p>
<p>The message shown by the error looks like this:</p>
<pre><code class="lang-plaintext">RenderBox was not laid out: RenderViewport#075d6 NEEDS-PAINT
'package:flutter/src/rendering/box.dart':
Failed assertion: line 2165 pos 12: 'hasSize'
</code></pre>
<p><strong>How might you run into this error?</strong></p>
<p>This error occurs when a widget tries to render without proper constraints. Each widget in Flutter needs to know how much space it has to work with.</p>
<p>Sometimes, the <code>RenderBox was not laid out</code> error is often caused by one of two other errors:</p>
<ul>
<li><p><code>Vertical viewport was given unbounded height</code></p>
</li>
<li><p><code>An InputDecorator...cannot have an unbounded width</code></p>
</li>
</ul>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">You can see an example of this error at <a target="_blank" href="https://github.com/nickf2k/flutter_common_constraint_errors/blob/main/lib/errors_page/renderbox_not_laid_out_page.dart"><strong>flutter_common_constraint_errors</strong></a></div>
</div>

<h2 id="heading-32-a-renderflex-overflowed-by-error">3.2 <code>A RenderFlex overflowed by</code> error</h2>
<p><strong>What does the error look like?</strong></p>
<pre><code class="lang-plaintext">The following assertion was thrown during layout:
A RenderFlex overflowed by 1188 pixels on the right.
</code></pre>
<p><strong>How might you run into this error?</strong></p>
<p>This error often occurs when a <code>Column</code> or <code>Row</code> has a child widget that isn't constrained in its size. For example:</p>
<pre><code class="lang-dart">Row(
      children: [
        <span class="hljs-keyword">const</span> Icon(Icons.error),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(<span class="hljs-string">'Title'</span>,
                style: Theme.of(context).textTheme.headlineMedium),
            <span class="hljs-keyword">const</span> Text(
              <span class="hljs-string">'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed '</span>
                  <span class="hljs-string">'do eiusmod tempor incididunt ut labore et dolore magna '</span>
                  <span class="hljs-string">'aliqua. Ut enim ad minim veniam, quis nostrud '</span>
                  <span class="hljs-string">'exercitation ullamco laboris nisi ut aliquip ex ea '</span>
                  <span class="hljs-string">'commodo consequat.'</span>,
            ),
          ],
        ),
      ],
    )
</code></pre>
<p><strong>How to fix it?</strong></p>
<p>Using Expanded or Flex widgets within Row or Column, or setting specific constraints using Container/SizedBox can help prevent this.</p>
<p>Here is the solution for the above code (See comment in code):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720667980174/e84e5e18-1780-426c-8a9e-95d345908632.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-33-vertical-viewport-was-given-unbounded-height-error">3.3 <code>Vertical viewport was given unbounded height</code> error</h2>
<p><strong>What does the error look like?</strong></p>
<pre><code class="lang-dart">The following assertion was thrown during performResize():
Vertical viewport was given unbounded height.
Viewports expand <span class="hljs-keyword">in</span> the scrolling direction to fill their container. In <span class="hljs-keyword">this</span> <span class="hljs-keyword">case</span>, a vertical viewport was given an unlimited amount of vertical space <span class="hljs-keyword">in</span> which to expand. This situation typically happens when a scrollable widget <span class="hljs-keyword">is</span> nested inside another scrollable widget.
</code></pre>
<p><strong>How might you run into this error?</strong></p>
<p>The error is often caused when a <code>ListView</code> (or <code>GridView</code>) is nested inside a <code>Column</code>. A <code>ListView</code> will occupy all the vertical space it can get unless restricted by its parent. However, a Column does not inherently limit the height of its children. The interaction of these behaviors results in the inability to establish the <code>ListView</code> ’s size.</p>
<p>See the example <a target="_blank" href="https://github.com/nickf2k/flutter_common_constraint_errors/blob/main/lib/errors_page/vertical_viewport_unbounded_page.dart">here</a></p>
<p><strong>How to fix it?</strong></p>
<p>To resolve this error, you need to define the height of the <code>ListView</code>. To ensure it occupies the remaining vertical space within the <code>Column</code>, wrap it in an <code>Expanded</code> widget (as illustrated in the example below). Alternatively, you can assign a fixed height with a <code>SizedBox</code> widget or a proportional height using a <code>Flexible</code> widget.</p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Solution of the example is the comment in code, check it again</div>
</div>

<h1 id="heading-4-conclusion">4. Conclusion</h1>
<p>Understanding and properly using constraints in Flutter is crucial to avoid common layout errors like ‘RenderBox was not laid out’, ‘A RenderFlex overflowed by’, and ‘Vertical viewport was given unbounded height’. By mastering constraints, you can create robust, error-free layouts and enhance your Flutter development skills. Keep experimenting and exploring to make the most out of Flutter’s powerful layout system.</p>
<hr />
]]></content:encoded></item></channel></rss>