Building a Networked Flutter App with the Dio Package

Nurettin Eraslan
3 min readJul 3, 2023

--

Flutter is a versatile framework for developing cross-platform mobile applications. When it comes to handling network requests, the Dio package provides a powerful and flexible solution. In this article, we will explore how to build a networked Flutter app using the Dio package. We will walk through the process step by step and provide a sample application to demonstrate its usage.

Step 1: Project Setup and Package Installation: To get started, create a new Flutter project using the following command:

flutter create dio_example_app

Open the project in your preferred code editor and add the necessary dependencies to the ‘pubspec.yaml’ file:

dependencies:
flutter:
sdk: flutter
dio: ^4.0.0

Save the file and run the following command in the terminal to fetch the packages:

flutter pub get

Step 2: Implementing the Networked Flutter App: Let’s implement a sample app that fetches data from an API using the Dio package. Open the ‘lib/main.dart’ file and replace the existing code with the following:

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

void main() {
runApp(DioExampleApp());
}

class DioExampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Dio Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: DioExamplePage(),
);
}
}

class DioExamplePage extends StatefulWidget {
@override
_DioExamplePageState createState() => _DioExamplePageState();
}

class _DioExamplePageState extends State<DioExamplePage> {
List<String> posts = [];

void fetchPosts() async {
try {
Response response = await Dio().get('https://jsonplaceholder.typicode.com/posts');
List<dynamic> data = response.data;

setState(() {
posts = data.map((post) => post['title']).toList();
});
} catch (error) {
// Handle error
print('Error: $error');
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dio Example'),
),
body: ListView.builder(
itemCount: posts.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(posts[index]),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: fetchPosts,
child: Icon(Icons.refresh),
),
);
}
}

Step 3: Running the Dio Example App: Save the changes and run the app using the command:

flutter run

The app will launch in the simulator or on a physical device. It displays a list of posts fetched from the JSONPlaceholder API. When you tap the floating action button, the app will make a GET request using Dio to retrieve the posts from the API. The response data is then displayed in a list view.

Conclusion: In this article, we explored how to build a networked Flutter app using the Dio package. We implemented a sample application that demonstrates fetching data from an API and displaying it in a list view. The Dio package provides a simple yet powerful way to handle network requests in Flutter, allowing you to build robust and efficient networked applications.

References:

Please note that the code provided is for illustrative purposes, and you may need to customize it based on your specific API endpoints and data requirements.

With the Dio package, you can easily integrate network functionality into your Flutter app, enabling seamless communication with APIs and fetching data in a structured manner.

--

--

Nurettin Eraslan
Nurettin Eraslan

No responses yet