2

I have a binary file I need to read its contents using retrofit for my flutter application.

I want to know if this could be possible or not. if yes, any links, please?

Otherwise, I need some recommendations.

Thanks in advance for your help

blue pine
  • 472
  • 6
  • 17
rania
  • 63
  • 6

2 Answers2

1

First of all, you have to specify your retrofit client and specify your API inside and specify ReturnType.bytes by DioResponseType annotation

I'll make a simple example for downloading the image from the https://www.phoca.cz web site and in the same way you can download any type of files

part 'rest_client.g.dart';

@RestApi(baseUrl: "https://www.phoca.cz/")
abstract class RestClient {
  factory RestClient(Dio dio, {String baseUrl}) = _RestClient;

  @GET("images/phocadownloadsite/phocadownload-category-view-bootstrap-mobile-mobile-view.png")
  @DioResponseType(ResponseType.bytes)
  Future<HttpResponse<List<int>>> downloadFile();
}

Generate a code by

flutter pub run build_runner run

Initialise a client

final dio = Dio(); 
final client = RestClient(dio);
final response = await client.downloadFile();

And you can obtain the image through Image.memory

Future<void> main() async {
  final dio = Dio();
  final client = RestClient(dio);
  final response = await client.downloadFile();
  WidgetsFlutterBinding.ensureInitialized();

  runApp(MaterialApp(
    home: Scaffold(
      body: Center(
        child: Image.memory(response.response.data),
      ),
    ),
  ));
}

enter image description here

powerman23rus
  • 1,297
  • 1
  • 9
  • 17
  • this means that if I change the picture url by the file one and then I follow all these steps this will work for me ? – rania Dec 07 '22 at 15:38
  • Downloading file - yes. Difference only in what you are planning to do with bytes of your file – powerman23rus Dec 07 '22 at 15:44
  • I have not juste to download file I have to read its content. Is this possible with this way ? – rania Dec 07 '22 at 15:49
  • You will obtain your file in **response.response.data** where will be located list of bytes of your file. You can do with those anything what you want: read, write, send and etc – powerman23rus Dec 07 '22 at 15:55
  • could you accept my answer, please, if I answered on your question – powerman23rus Dec 07 '22 at 15:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250254/discussion-between-rania-and-powerman23rus). – rania Dec 08 '22 at 08:37
  • Thank u so much for sharing! After 1 entire day struggling with retrofit I could make it work this way. I was trying to receive an HttpResponse, but the generated .g.dart file was throwing error. Another crucial thing was the @DioResponseType(ResponseType.bytes) tag...this tag is not mentioned on the retrofit documentation...how did u know of its existence?...I was trying to set the response type on the dio options directly on the dio instance, but it never worked...I was always receiving a string. – Ramiro G.M. Apr 24 '23 at 15:40
0

The answer given by powerman23rus works perfectly with retrofit, but if you are looking for a package that requires less code, u can try the http package (https://pub.dev/packages/http) which doesn't require tags or generated files. You can receive an image like this:

import 'package:http/http.dart' as http;

late http.Response response;
  response = await http
      .get(
    Uri.parse("https://www.example.com/Cards/GetRandom.png"),
    headers: {"Authorization":"Bearer "+token}
  )
      .timeout(const Duration(seconds: 7));
Uint8List image = response.bodyBytes;

Once obtained, you can use this widget to render the image:

Image.memory(image)
Ramiro G.M.
  • 357
  • 4
  • 7