Implementing Pull-To-Refresh In Flutter

Implementing Pull-To-Refresh In Flutter

RefreshIndicator to implement the pull to refresh mechanism.

In this blog, we shall discuss about Pull-To-Refresh feature. Most of the apps that are made today come with API integration, sometimes when there is a change in data of the API the change might not change in realtime in the app so we need to load the data again in our app. Flutter provides us RefreshIndicator widget that can be used to implement the same feature.

RefreshIndicator is a widget that provides us onRefresh a property that we can use for reloading the API data. When the refresh operation is finished it returns a future.

Table of content

Fetching JSON data

Decode data

Fruit Class

Creating MyApp class

Displaying API data

Using RefreshIndicator

Refresh function

main.dart file

Fetching JSON data

  • Here we have created a function fetchFruit()that will fetch the JSON data from the given URL.
Future fetchFruit() async {
  final response = await http.get('url');
  if (response.statusCode == 200) {
    //decode_JSON_data
  } else {
    throw Exception('Unable to fetch data from the REST API');
  }
}

Decode data

  • We can use the decode method to decode the responseBodyand then we can map the parsedto list.
List<Fruit> decodeFruit(String responseBody) {
  final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
  return parsed.map<Fruit>((json) => Fruit.fromMap(json)).toList();
}

flutter-widget flutter refresh-indicator

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Google's Flutter 1.20 stable announced with new features - Navoki

Google has announced new flutter 1.20 stable with many improvements, and features, enabling flutter for Desktop and Web

What is Flutter and why you should learn it?

Flutter is an open-source UI toolkit for mobile developers, so they can use it to build native-looking Android and iOS applications from the same code base for both platforms. Flutter is also working to make Flutter apps for Web, PWA (progressive Web-App) and Desktop platform (Windows,macOS,Linux).

Flutter: Stateful Widget vs. Stateless Widget

Flutter is all about widgets. If you want to develop an application you have to first decide which widgets you are going to use. Each widget has its state. Flutter: Stateful Widget vs. Stateless Widget

Adobe XD plugin for Flutter with CodePen Tutorial

Recently Adobe XD releases a new version of the plugin that you can use to export designs directly into flutter widgets or screens.

Flutter Container Widget - Flutter Widget Essentials #2

Flutter Container Widget - Flutter Widget Essentials #2