/flutter_simple_http_Get_RestAPI

a simple flutter program for fetching RESTful API response from "https://jsonplaceholder.typicode.com/posts/" using "http" package. Null-safety observed too.

Primary LanguageC++

Project Name:

App

Description :

A simple flutter app for performing GET request on https://jsonplaceholder.typicode.com/posts/ endpoint using flutter http package from pub.dev.


Snippet source code below :

services/network/http_service.dart

import 'dart:convert';

import 'package:app/model/post_model.dart';
import 'package:http/http.dart' as http;

class HttpService {
  String baseUrl = 'https://jsonplaceholder.typicode.com/posts';
  List<PostModel> allPost = []; //a Global list to hold all Post data.

  Future<List<PostModel>> getData() async {
    var url = Uri.parse(baseUrl);
    var res = await http.get(url);

    if (res.statusCode == 200) {
      List jsonResponseBody = jsonDecode(res.body);

      for (var eachPost in jsonResponseBody) {
        print('post-nọ: ${eachPost['id']}');
        print('user-id:  ${eachPost['userId']}');
        print('post-title:  ${eachPost['title']}');
        print('post-body:  ${eachPost['body']}');
        print('');

        //send "eachPost" value to "Post.fromJson()" factory constructor then,
        //append all to "allPost" list using .add() ;  a dart's List built-in method.

        allPost.add(PostModel.fromJson(eachPost));
      }
    }
    return allPost;
  }
}