Widget testing: Dealing with Renderflex Overflow Errors

Search for a command to run...

No comments yet. Be the first to comment.
Here we will cover various aspects of unit, widget and integration testing in Flutter
Today we going to look at a great utility provided by Flutter's testing framework which gives us a lot more power when it comes to accurately test our widgets. Very often widgets can very simply be tested using find.byType, find.text and find.byKey. ...
The Problem Wasn’t Triage Most AI workflows in software engineering still keep the human directly in the middle of triage. The AI might help write code. It might explain a stack trace. It might summar

There’s been a steady undercurrent of people switching tools lately. Copilot to Claude. Claude to something else. Codex quietly entering more workflows. A lot of confidence in different directions, of

It’s been said to me that at times my responses can come across as a “no” first-unintentionally, of course, but real all the same. Not long ago, someone on my team shared an idea they were exploring. I began with a caution, meaning to help them avoid...

The opening sentence of any message often carries more weight than everything that follows. I’ve learned this the hard way. Recently, a report shared something he wanted to pursue. I was supportive — I really was — but my first response came as a cau...

One of the things I’ve always been comfortable with is admitting when I don’t know something. To me, it’s one of the real distinctions between the more and less experienced engineers — and honestly, people in general. When you’re earlier in your care...

We've all certainly seen it in our UI before, "A RenderFlex overflowed by...", and we all know this error means that our UI is painting beyond the bounds of our available real estate.
When this is on the screen, it's very easy to fix, but what about when you are unit testing your widgets and you run into this?
This is something I have run into a few times and for the most part, have just ignored it in tests, I condor widget testing a bit of a lower priority, for the most part, I believe their testing is best done as part of the journey or integration testing.
That being said, there are times when business logic sits within a widget, and at that point, it can be valuable to spend the time to write an adequate unit test.
In the past trying to hack around with the virtual UI has yielded some useable results, but when this error popped up more recently I went scouring around for a more reliable solution.
Full disclosure, this was mostly the brainchild of Remi Rousselet, I am just helping to share it to make other developers' lives that little bit easier.
I always keep a test_helpers.dart file for my unit testing, and this is my latest addition to that list of helpers:
void ignoreOverflowErrors(
FlutterErrorDetails details, {
bool forceReport = false,
}) {
bool ifIsOverflowError = false;
bool isUnableToLoadAsset = false;
// Detect overflow error.
var exception = details.exception;
if (exception is FlutterError) {
ifIsOverflowError = !exception.diagnostics.any(
(e) => e.value.toString().startsWith("A RenderFlex overflowed by"),
);
isUnableToLoadAsset = !exception.diagnostics.any(
(e) => e.value.toString().startsWith("Unable to load asset"),
);
}
// Ignore if is overflow error.
if (ifIsOverflowError || isUnableToLoadAsset) {
debugPrint('Ignored Error');
} else {
FlutterError.dumpErrorToConsole(details, forceReport: forceReport);
}
}
I have updated it somewhat from the original solution, it was written pre-null-safety and I also added in the check for errors loading local assets.
What this helper is doing is preventing the test runner from falling out when the test runs into one of these errors. I feel it quite silly that at the very least the RenderFlex issues are not an exception in tests, Unit tests exist to test logic, how well a widget paints within a headless environment should have no bearing on the test itself.
To make use of this helper, you simply need to include it as part of your test with FlutterError.onError = ignoreOverflowErrors;, see the following example:
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
testWidgets('Widget Renders Correctly', (tester) async {
FlutterError.onError = ignoreOverflowErrors;
await tester.pumpApp(const PrimaryHeader(child: Text('Test')));
final titleFinder = find.text('Test');
expect(titleFinder, findsOneWidget);
});
}
For it to work, it needs to be assigned in the specific test, it cannot be assigned in setUp or setUpAll.
I hope you found this interesting, and if you have any questions, comments, or improvements, feel free to drop a comment. Enjoy your Flutter development journey :D
If you enjoyed it, a like would be awesome, and if you really liked it, a cup of coffee would be great.
Thanks for reading.
Wish to carry on with the topic of Unit Testing, take a look at: