GetX Post Api Call in Flutter

GetX Post :

GetX Post api call implementation in flutter app is discussed in this blog with a real time api. Post api calls are almost used in every app and library’s to send the data.

These POST api’s sometimes require encoding and why do we require encoding of api call do all the api calls are encoded ??

API calls are typically encoded for two primary reasons: security and interoperability.

One reason why API calls are encoded is to enhance security. When data is transmitted over a network, it can be intercepted and read by malicious actors. By encoding the data, it is much harder for someone to interpret the contents of the data, thereby improving the security of the data being transmitted.

Another reason why API calls are encoded is to ensure interoperability. Different programming languages and systems may use different formats for encoding data. By encoding data in a standardized format, such as JSON or XML, different systems can communicate with each other more easily and accurately.

Overall, encoding API calls is an important practice for ensuring the security and interoperability of API-based systems.

 

GetX Post Video Tutorial :

Go through the below tutorial for more detailed implementation.

 

pubspec.yaml :

Add get library to the project with latest version

 

dependencies:
  flutter:
    sdk: flutter

  get: ^4.6.5

 

 

main.dart :

In this class we are going to specify the basic structure of the app and add a button to make api call.

 

Specify the import

import 'package:get/get_connect.dart';

 

 

Declare a getx variable using which we can make api request

final getx = GetConnect();

 

 

Now add the body parameters which are to be posted to api

final data = {"Name": "Abhishek", "UserType": "Admin"};

 

 

Specify api call with getx similar is possible with flutter http.post example as well

getx.post("https://reqres.in/api/users", data);

 

 

As this a tutorial we are just printing the response

response.then((value) => {print(value.body)});

 

 

Create a async method as below

void fetchData() async {
  final data = {"Name": "Abhishek", "UserType": "Admin"};

  final response = getx.post("https://reqres.in/api/users",
      data);

  if(kDebugMode){
    response.then((value) => {print(value.body)});
  }
}

 

 

Full Code :

Flutter getx post request full code

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get_connect.dart';

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

final getx = GetConnect();

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text("GetX Post Api Call"),
        ),
        body: Center(
          child: TextButton(
            onPressed: () {
              fetchData();
            },
            child: const Text("Fetch Data"),
          ),
        ),
      ),
    );
  }
}

void fetchData() async {
  final data = {"Name": "Abhishek", "UserType": "Admin"};

  final response = getx.post("https://reqres.in/api/users",
      data);

  if(kDebugMode){
    response.then((value) => {print(value.body)});
  }
}

 


 

If you have any query’s in this tutorial on flutter post request example 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 deeplink Enhance | Go Router

Flutter deeplink : Flutter deeplink, A deeplink point's to a specific page in website or mobile app rather than opening...

Close