📒
Mobile Development Reference-TCE-C01
  • Welcome
  • Introduction
    • Project Description
  • First Steps
    • Creating Project
    • Setting up Workspace
      • Project structure
      • Setting up the app
  • Routing
    • Setting up auto route
    • Creating the first screen
    • Setting up router
    • Adding router to Material App
  • Products Feature
    • Creating models
      • Introduction to freezed
      • Introduction to JSON annotations
      • Creating products model
    • Creating products cubit
      • Creating Products States
      • Creating Products Cubit
      • Implementing Get Products Functionality
    • Creating products repository
      • Setting up Dio
      • Making your first http request with DIO
    • Implementing Products Screen UI
      • Adding Products Grid
      • Adding Products Tile
      • Add Products Error Widget
    • Consuming the products cubit
      • Setting up Dependency injection
      • Creating Bloc Provider for ProductsCubit
      • Mapping Products Cubit states to UI
  • Testing
    • Widget Testing
    • Unit Testing
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Products Feature
  2. Creating products repository

Making your first http request with DIO

Import the DIO package

import 'package:dio/dio.dart';

Making your first http request with DIO

var dio = Dio();
Response response = await dio.get('https://google.com');
print(response.data);

The above example will make a request to http://google.com and print the result.

Additional Examples

Performing a GET request:

import 'package:dio/dio.dart';

Response response;
Dio dio = new Dio();

response = await dio.get("/test?id=12&name=wendu");
print(response.data.toString());

// Optionally the request above could also be done as
response = await dio.get("/test", queryParameters: {"id": 12, "name": "wendu"});
print(response.data.toString());

Performing a POST request:

response = await dio.post("/test", data: {"id": 12, "name": "wendu"});

Performing multiple concurrent requests:

response = await Future.wait([dio.post("/info"), dio.get("/token")]);

Downloading a file:

response = await dio.download("https://www.google.com/", "./xx.html");

Dio APIs

Creating an instance and set default configs.

You can create an instance of Dio with an optional BaseOptions object:

Dio dio = new Dio(); // with default Options

// Set default configs
dio.options.baseUrl = "https://www.xx.com/api";
dio.options.connectTimeout = 5000; //5s
dio.options.receiveTimeout = 3000;

// or new Dio with a BaseOptions instance.
BaseOptions options = new BaseOptions(
    baseUrl: "https://www.xx.com/api",
    connectTimeout: 5000,
    receiveTimeout: 3000,
);
Dio dio = new Dio(options);

PreviousSetting up DioNextImplementing Products Screen UI

Last updated 4 years ago

Was this helpful?