ShrinkWrap vs Slivers | Decoding Flutter
Summary
TLDRIn this video, Craig Labenz explains how Flutter developers often encounter performance issues when using ListViews within ListViews. He demonstrates how setting 'Shrink Wrap' to true can resolve the error but introduces potential performance problems, especially with large or animated lists. Craig recommends replacing nested ListViews with SliverLists in a CustomScrollView, which efficiently builds only the visible widgets. This change leads to better performance, reducing dropped frames and stuttering. The video emphasizes the importance of efficient widget rendering for smooth user experiences in Flutter apps.
Takeaways
- π New Flutter developers often start by learning the ListView widget, which is used for scrolling widgets.
- π Nested ListViews in Flutter can cause performance issues due to unbounded dimensions, resulting in errors.
- π A common suggestion to fix the issue is setting the ShrinkWrap property to true, but this can lead to performance problems.
- π ShrinkWrap forces Flutter to evaluate an entire list of widgets upfront, which can cause jank and stuttering in large lists.
- π ShrinkWrap is especially problematic if inner lists contain a lot of widgets or animations, increasing the risk of dropped frames.
- π A better solution is to replace the outer ListView.builder with a CustomScrollView.
- π Inside the CustomScrollView, ListViews should be replaced with SliverLists for better performance.
- π SliverLists use delegates, which may sound complicated but are easy to work with, especially SliverChildBuilderDelegate.
- π When migrating from ListViews to SliverLists, the indexed widget builder function is transferred to the SliverChildBuilderDelegate.
- π The result is a more efficient UI where inner lists only build widgets currently in the viewport, improving performance.
- π Testing the changes shows a clear performance improvement with Slivers, as seen in DartPad with build method print statements.
Q & A
What is the main issue discussed in the video?
-The main issue is the performance problem that occurs when using nested ListView widgets in Flutter, particularly when the inner ListViews do not have bounded dimensions.
What does Flutter's error message suggest when encountering the nested ListView problem?
-Flutter's error message suggests setting the 'shrinkWrap' property to true as a potential solution to the problem of unbounded dimensions.
What are the drawbacks of setting 'shrinkWrap' to true?
-Setting 'shrinkWrap' to true can significantly impact performance, especially if the inner ListViews contain many widgets or animations, potentially causing dropped frames and stutters.
How is the concept of a 'viewport' explained in the video?
-The viewport is compared to a porthole on a cruise ship, where you can only see a few items (widgets) at a time. The rest of the list is off-screen and potentially infinite, just like fish swimming by in the analogy.
Why can't Flutter handle nested ListViews directly?
-Flutter cannot handle nested ListViews directly because there are now two portholes (viewports), and Flutter can't determine the full size of the inner list upfront, leading to errors and performance issues.
What is the role of 'shrinkWrap' in solving the problem?
-Setting 'shrinkWrap' to true forces the inner ListView to evaluate its full size upfront, but this can cause performance problems when dealing with large or complex lists.
What is the recommended solution to improve performance with nested lists?
-The recommended solution is to replace nested ListViews with SliverLists inside a CustomScrollView, which renders the widgets efficiently by only building the ones visible in the viewport.
What is the purpose of a 'SliverList'?
-A SliverList is a scrollable widget in Flutter that allows for efficient rendering by only building the items that are currently visible in the viewport, which helps prevent performance issues.
What is the role of the 'SliverChildBuilderDelegate'?
-The 'SliverChildBuilderDelegate' is used to build the items for a SliverList, similar to how ListView.builder works. It only renders items as needed based on the current viewport, improving performance.
What performance improvement can be expected by switching to SliverLists?
-By switching to SliverLists, you can expect a significant performance improvement as Flutter only renders widgets that are currently visible in the viewport, reducing the load on the system and preventing dropped frames and stuttering.
Outlines

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowMindmap

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowKeywords

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowHighlights

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowTranscripts

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowBrowse More Related Video

98% Flutter Developer Don't Know These 10 EPIC UI Packages π± | Redesign Your UI Like a Pro

My honest opinion about SwiftUI vs Flutter vs React Native to build iOS apps

Flutter Tutorial for Beginners #23 - Maps & Routing

Flock & Nest : New Flutter Community Projects by Matt Carroll, @SuperDeclarative

FLUTTER 09 Image Widget (Tutorial Flutter Bahasa Indonesia)

Is it worth learning Flutter in 2024 and Beyond? | Explained | Senior SWE
5.0 / 5 (0 votes)