Simple Get Api Call Using Dio and Htttp Package

         
 Get Api Call Using Dio

import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import '../model/post_model.dart'; class PostDioApi extends StatefulWidget { const PostDioApi({super.key}); @override State<PostDioApi> createState() => _PostDioApiState(); } class _PostDioApiState extends State { List<PostModel> postList = []; @override void initState() { getPostApi(); super.initState(); } @override Widget build(BuildContext context) { return Scaffold( body: ListView.builder( itemCount: postList.length, itemBuilder: (context, index) { return ListTile( title: Text(postList[index].title), subtitle: Text(postList[index].body), ); }, ), ); } Future<void> getPostApi() async { final dio = Dio(); try { final response = await dio.get( 'https://jsonplaceholder.typicode.com/posts', ); if (response.statusCode == 200) { setState(() { postList = (response.data as List) .map((json) => PostModel.fromJson(json)) .toList(); }); } else { print('Error: ${response.statusCode}'); } } catch (e) { print('Error: $e'); } } }

         
 Get Api Call Using Http

import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_apis/model/post_model.dart'; import 'package:http/http.dart' as http; class PostHttpApi extends StatefulWidget { const PostHttpApi({Key? key}) : super(key: key); @override State<PostHttpApi> createState() => _PostHttpApiState(); } class _PostHttpApiState extends State { List<PostModel> postList = []; @override void initState() { getPostApi(); super.initState(); } @override Widget build(BuildContext context) { return Scaffold( body: ListView.builder( itemCount: postList.length, itemBuilder: (context, index) { return ListTile( title: Text(postList[index].title), subtitle: Text(postList[index].body), ); }, ), ); } Future getPostApi() async { try { final response = await http.get( Uri.parse('https://jsonplaceholder.typicode.com/posts'), ); if (response.statusCode == 200) { print('response is successful'); List<dynamic> jsonResponse = jsonDecode(response.body); setState(() { postList = jsonResponse.map((json) => PostModel.fromJson(json)).toList(); }); } else { print('response is Failed'); } } catch (e) { print('Error: $e'); } } }

         
 Post Model Class

class PostModel { final int userId; final int id; final String title; final String body; PostModel({ required this.userId, required this.id, required this.title, required this.body, }); factory PostModel.fromJson(Map<String, dynamic> json) { return PostModel( userId: json['userId'], id: json['id'], title: json['title'], body: json['body'], ); } }

 
Visit Link for the Source Code