Home > Mobile >  How to fetch the data from the API and store into a variable in flutter?
How to fetch the data from the API and store into a variable in flutter?

Time:07-22

I need to fetch some data from the API and the data I need to store the data in a variable.

CodePudding user response:

Add this in pubspec.yaml

dependencies:
      dio: ^4.0.6

//Code in dart file

import 'package:dio/dio.dart';
void getHttp() async {
  try {
    var response = await Dio().get('http://www.google.com');
    print(response);
  } catch (e) {
    print(e);
  }
}

CodePudding user response:

You should consider to create an object where you want to put your json data. more likely you use http package for retrieving data from api then store it in your created object. usually api data formatted as json format, in that case you use dart:convert for Serializing JSON. For displaying the output you're about to use the getter to retrieve the data from your object.

  • Related