Flutter Stuff

How to Get Plain Response from Dio Request in Flutter

**Title:** How to Get Plain Response from Dio Request in Flutter

**Introduction:**

As a Flutter developer, you’re probably familiar with the power of Dio, a popular HTTP client library that makes it easy to make requests to your favorite APIs. But have you ever needed to get a plain response from a Dio request? Maybe you want to parse the response manually, or perhaps you need to handle errors in a specific way. Whatever the reason, I’m here to show you how to get plain responses from Dio requests in Flutter.

**What is a Plain Response?**

Before we dive into the details, let’s quickly define what we mean by a “plain response”. In the context of Dio, a plain response refers to a response that’s not JSON decoded or converted to a specific object. Instead, you get the raw response data, which can be a string, a buffer, or even a file.

**Using Dio’s `Response` Object**

To get a plain response from Dio, you can use the `Response` object provided by the library. The `Response` object contains a property called `data`, which holds the raw response data.

Here’s an example of how to use the `Response` object to get a plain response:
“`dart
import ‘package:dio/dio.dart’;

Future getToken() async {
final dio = Dio();
final response = await dio.get(‘https://example.com/token’);

final plainResponse = response.data; // Get the plain response data

// Use the plain response data as needed
// For example, you can print it to the console:
print(plainResponse);
}
“`
In this example, we create a Dio instance and make a GET request to a URL. The `Response` object is then stored in the `response` variable. We can access the plain response data using the `data` property, and store it in the `plainResponse` variable.

**Using Dio’s `Stream` Interface**

Another way to get a plain response from Dio is by using the library’s `Stream` interface. This method allows you to handle async responses and get plain response data on the fly.

Here’s an example of how to use Dio’s `Stream` interface to get a plain response:
“`dart
import ‘package:dio/dio.dart’;
import ‘package:flutter/material.dart’;

class GetTokenScreen extends StatefulWidget {
@override
_GetTokenScreenState createState() => _GetTokenScreenState();
}

class _GetTokenScreenState extends State {
final dio = Dio();
late Stream _plainResponseStream;

@override
void initState() {
super.initState();
_plainResponseStream = dio.getUri(‘https://example.com/token’).stream
.transform(utf8.decoder)
.transform(UTF8Codec(errorHandling: ReplaceFaultyCodeUnitsReplacement));

_plainResponseStream.listen((data) {
// Handle the plain response data as needed
// For example, you can print it to the console:
print(data);
});
}
}
“`
In this example, we create a Dio instance and make a GET request to a URL using the `stream` method. We then transform the response stream using a UTF-8 decoder and a custom error handler. Finally, we listen to the stream and handle the plain response data on the fly.

**Conclusion:**

Getting plain responses from Dio requests in Flutter is straightforward. By using the `Response` object or the `Stream` interface, you can access the raw response data and handle it as needed. Whether you need to parse the response manually, or handle errors in a specific way, Dio provides the tools you need to do so. Happy coding!

Leave a Comment

Scroll to Top