Flutter Dio GET Network Call Implementation

Flutter Dio GET :

Almost every app has a functionality, network calls are utilised sometimes to make the work done. But here the point is how fast does the network call works.

GET is the basic, most used API request we just browse through any website then the request made is GET.And again based on the task we perform in the website api request might change form POST, PUT so on..

Network call implementation was a big task to be performed but this tutorial will simplify it to you so that you can perform it in just 1 min.

In this part of the tutorial we will be seeing through the implementation of the fastest way to fetch the data in your flutter app using Dio library.

You can go through below tutorial for more detailed updates on GET implementation and for more API request refer dio playlist

 

Flutter Dio GET Video Tutorial :

Go through the below tutorial for more details on dio implementation

 

pubspec.yaml :

We need to add the latest version of dio library and make sure it is sync properly with flutter version.

dependencies:
  flutter:
    sdk: flutter
  dio: ^4.0.6

 

main.dart :

We will create a method for fetching data and will be utilising it in our code.You can also try to implement concurrent network calls with both GET, POST.

We can fetch the status code first so that we can know the success of failure of the api call.Can find more details on status codes here.

 

void fetchData() async{
  var dio = Dio();
  var response = await dio.get("https://jsonplaceholder.typicode.com/posts");
  print(response.statusCode);
  print(response.data.toString());
}

 

Providing the full code for flutter dio library implementation. This is a basic implementation of get api request and you can customize it according to your requirement.

 

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

void main(){
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextButton(onPressed: (){
                fetchData();
              }, child: const Text("Post Data"))
            ],
          ),
        ),
      ),
    );
  }
}

void fetchData() async{
  var dio = Dio();
  var response = await dio.get("https://jsonplaceholder.typicode.com/posts");
  print(response.statusCode);
  print(response.data.toString());
}

 

If you have any query’s in flutter dio GET API request implementation do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates.

 

Show Buttons
Hide Buttons
Read previous post:
Flutter Drift Database Implementation

Flutter Drift database : Flutter Drift database is a reactive library used to store data locally built on top of...

Close