Skip to content

Flutter Bloc List Network

Flutter Bloc List Network

Here is the evolution of my personal – first – on-hand experience – with Flutter Bloc List Network example that uses different techniques and approaches. I’ve used a product that I’m developing – that is available for sell – with different variations. My goal is to include an app version in the near future.

Before looking at the network versions, read the:

Flutter – Loading list – with Basic Network

The Example code is located here: https://github.com/tomavelev/dev_examples/tree/master/bloc/lib_bloc_basic_network. It is a minimal app that extends the previous – in memory examples – with data – loaded from server. The most basic way to access resources from the Internet is with the http dart package. In this case – you/me/the client developer/ – is responsible for all and manually – passing the URL, response parsing, data conversion, error handling.

Flutter – Loading list – with Dio

The next Evolution is to wrap the http with the dio package. The evolution the code could be seen here: https://github.com/tomavelev/dev_examples/tree/master/bloc/lib_bloc_network_dio.

Bonus feature of the dio package are the interceptors. With them – You could handle in a single place – authentication, caching, logging or some custom network logic.

Flutter Bloc List Network + Retrofit

The repeating part in the upper examples is the response handling and data transformation. You could delegate this functionality to packages like retrofit. It’s a library already battle tested in Android Native Development. I’ve seen it in practice – with apps that I’ve participated – deployed in production in previous companies I’ve worked for.

The package for flutter is located here: https://pub.dev/packages/retrofit. A thing to get into your head is – it is a generator library. Second thing that I’ve found is – it requires from the back-end to have set – the Content-Type Header. https://github.com/tomavelev/dev_examples/blob/master/bloc/lib_bloc_network_retrofit_json/network.dart. Otherwise – it does not convert the String in the response out of the box.

I’ve written about Code Generation – in philosophical terms – in a previous post: https://programtom.com/dev/2022/01/13/levels-of-code-generation/. I’ve developed several code, no-code tools with minimal success. Maybe it is time to dive in Code Generation from another point of view – developer experience while coding – and less – idealistic, business, marketing, end user requirements or philosophy.

Leave a Reply

Your email address will not be published.