Flutter Display Data from API with Skelton Loading & Custom Error Message

Preview

Hello, today we will learn how to show the Skelton loading effect while fetching data also if there is any error how to show custom error message.

Video Tutorial

Let’s get started, what we try to do is fetching categories from API, for that we create and there is a method called which return Future list of categories. Here we made a request to our API, if the response statusCode is 200 then we convert the response into a list of Categories.

CategoryService

It’s time to show the categories to our .

DiscoveryScreen.dart

As we know return , On flutter we already have a widget called which is utilizes the result of a Future to build itself. Inside the FutureBuilder we have to pass 2 parameter one is , where we need to pass the function that return a future, and the function is . Another one is . For now return a Text.

First I want to show a loading so that the user understand something going on under the hood. If the is waiting then it shows the loading indicator right now which is a .

show a loading
Loading Preview

For batter user experience instead of we can use shimmer loading effect, Replace widget with and with .

Skelton loading effect
DiscoverCategoriesSkelton
Preview with Skelton Loading

If you want to learn how to create those shimmer effect check this👇

We are done with the loading now it’s time to show the categories, if snapshort has data then the data will be the list of categories, create a variable called then set the value to . To display the categories inside an widget define then sent the to and will be the , (This widget is created by me.)

Show caregories
Preview

We are almost done but what happened if there any error occurred? it’s better if you notify the user there is an error.

Show Error Message
Error preview

You may like 👇

DiscoverScreen Complete Code ❤️

Hope we learn something new, Check out our Premium E-commerce UI kit for flutter. 🔥👇

Thank you, have a great day!

Thumbnail

--

--

Want to improve your flutter skill? Join our channel, learn how to become an expert flutter developer and land your next dream job!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
The Flutter Way

Want to improve your flutter skill? Join our channel, learn how to become an expert flutter developer and land your next dream job!