I am not happy in my new job

When you get a new job it’s easy to feel when things aren’t right when you know yourself and have a bit of experience in the workplace. You can feel it in the air and when you speak to people. It’s a…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to unit test your Flutter loading widgets

Have you ever asked how could you test your loading Widgets the good way? We’ll try to answer this question in this article.

Let’s create a basic example building this simple page.

Loading Page

To check if the CircularProgressIndicator is drawn, I think we all have started this way.

Our first test

Running this code, we face an issue :

pumpAndSettle timed out

After pumping our testing widget, we have to call pumpAndSettle to ensure every sub widgets have been drawn. Here, CircularProgressIndicator is constantly redrawing so that pumpAndSettle timed out.

So, what’s the solution?

CircularProgressIndicator has a value property that can be set to prevent this constant rebuilding. We could add an argument to LoadingPage to set the value to 0 so that the tests succeed.

A little fix

In our test, we just have to add (isLoading:false) to LoadingPage and we’re good ! The test passed.

But now, what if I tell you that there is a way to remove this ugly isLoading parameter?

To get rid of the isLoading parameter, we’ll use the Platform.environment object that is available with dart:io.

We’ll create a mixin so that we can easily use it in every loading Widgets we’ll use.

Our test mixin

And now we just have to change our LoadingPage to use it.

And voilà, we remove the isLoadingParameter and we can run our fist test without any problem.

Thank you for your attention, don’t forget to subscribe and clap 👏

Add a comment

Related posts:

Claypot frog porridge

This is one of our favorite dish when we were living in Singapore.. “Claypot frog porridge” is published by muggleyoung.

Who Can Use A Text Reader?

Did you know the Text-To-Speech tools? the truth is that this kind of software is not new. The idea of text-to-speech may be new to us, but did you know that the first computer systems based on…

How Intel enhanced photorealism using machine learning techniques

Enhancing photorealism has obsessed the computer graphics world for years which has allowed us to see numerous papers, experiments, and even tricks to improve it in recent times. Some of them…