A package help you to make api call and handle error faster, also you can check for internet before call api.

Last update: Jun 18, 2020

Pub Version GitHub repo size

http_solver

##not for production use, only for learning purpose.

A package help you to make api call and handle error faster, also you can check for internet before call api.

Make your model

BaseModelForHttpSolver is a abstract class, your model should implements it.

class YourModel implements BaseModelForHttpSolver{}

Get data from your api

getFromApi static method take YourModel and your Api url and return Future and it is have some option parameter checkInternet, headers for Api.

YourModel dataInModel = await HttpSolver.getFromApi(YourModel(), apiUrl, checkInternet: true);

Post to api

getFromApi static method take YourModel and your Api url and return Future and it is have some option parameter checkInternet, headers for Api.

YourModel dataInModel = await HttpSolver.postToApi(YourModel(), apiUrl,body: body , checkInternet: true);

convert model to either

toEither() extension on Future to handle all error and return Either<Failure, YourModel>.

Future<YourModel> dataInModel =  HttpSolver.getFromApi(YourModel(), apiUrl, checkInternet: true);
Either<Failure, YourModel> modelOrError= dataInModel.toEither();

Using Either With UI

modelOrError.fold((failure) => Text("${failure.message}"), //if has error show Text with failure message
(model) => Text(post.title)), //else show Text data

Full Example

       import 'dart:collection';
       import 'dart:convert';
       import 'package:flutter/material.dart';
       import 'package:http_solver/http_solver.dart';

       void main() => runApp(MyApp());

       class MyApp extends StatelessWidget {
         @override
         Widget build(BuildContext context) {
           return MaterialApp(
             home: HomePage(),
           );
         }
       }

       class HomePage extends StatefulWidget {
         Either<Failure, Post> _data;
         final url = "http://www.mocky.io/v2/5e3c29393000009c2e214bf8";

         @override
         _HomePageState createState() => _HomePageState();
       }

       class _HomePageState extends State<HomePage> {
         @override
         Widget build(BuildContext context) {
           return Scaffold(
               floatingActionButton:
                   FloatingActionButton(onPressed: _getEither, child: Text('Get')),
               body: Center(
                 child: (widget._data == null)
                     ? Text('No Data Yet')
                     : widget._data.fold((failure) => Text("${failure.message}"),
                         (post) => Text(post.title)),
               ));
         }

         void _getEither() async {
           widget._data =        await HttpSolver.getFromApi(Post(), widget.url, checkInternet: true).toEither();
           setState(() {});
         }
       }

       class Post implements BaseModelForHttpSolver {
         final int id;
         final int userId;
         final String title;
         final String body;

         Post({
           this.id,
           this.userId,
           this.title,
           this.body,
         });

         Post fromJson(String source) {
           Map<String, dynamic> jsonData = (json.decode(source));
           if (jsonData == null) return null;
           return Post(
             id: jsonData['id'],
             userId: jsonData['userId'],
             title: jsonData['title'],
             body: jsonData['body'],
           );
         }

         @override
         Map<String, dynamic> toJson() {
           final Map<String, dynamic> data = HashMap<String, dynamic>();
           data['id'] = this.id;
           data['userId'] = this.userId;
           data['title'] = this.title;
           data['body'] = this.body;
           return data;
         }
       }

Developer

GitHub

https://github.com/AbdOoSaed/http_solver
You might also like...

dos downloader app is developed for downloading video. You can download video from YouTube and Facebook. You can also play video on background

dosdownloader Dos downloader app is developed for downloading video. You can download video from YouTube and Facebook. You can also play video on back

Dec 8, 2021

this app will help you to check BMI.

bmicalfinal A new Flutter application. Getting Started This project is a starting point for a Flutter application. A few resources to get you started

Dec 15, 2021

Bac-App-Flutter - Flutter application where you can read PDF locally or by internet and is saved

Bac-App-Flutter - Flutter application where you can read PDF locally or by internet and is saved

Bac App - Flutter Flutter application where you can read PDF locally or by inter

Jun 7, 2022

Flutter plugin for selecting images from the Android and iOS image library, taking new pictures with the camera, and edit them before using such as rotation, cropping, adding sticker/text/filters.

Flutter plugin for selecting images from the Android and iOS image library, taking new pictures with the camera, and edit them before using such as rotation, cropping, adding sticker/text/filters.

advance_image_picker Flutter plugin for selecting multiple images from the Android and iOS image library, taking new pictures with the camera, and edi

Aug 9, 2022

A Dart package to handle HTTP services

http_services A package to support the creation of Http services in a Dart application. Features convenient methods to perform HTTP requests disposing

Jul 27, 2021

Flutter package to simplify pagination of list of items from the internet.

Flutter package to simplify pagination of list of items from the internet.

PaginationView Installing In your pubspec.yaml dependencies: pagination_view: ^2.0.0-nullsafety.0 import 'package:pagination_view/pagination_view.da

Aug 2, 2022

Flutter package to simplify pagination of list of items from the internet.

Flutter package to simplify pagination of list of items from the internet.

PaginationView Installing In your pubspec.yaml dependencies: pagination_view: ^2.0.0-nullsafety.0 import 'package:pagination_view/pagination_view.da

Aug 2, 2022

Related

Readky is a Free Flutter News App Starter Template that can help you develop a News application much faster.
Readky is a Free Flutter News App Starter Template that can help you develop a News application much faster.

Readky. Introduction Readky is a Free Flutter News App Starter Template that can help you develop a News application much faster. You just need to add

Aug 5, 2022
Hungry is a Free Flutter Recipe App Starter Template that can help you develop a Recipe application much faster.
Hungry is a Free Flutter Recipe App Starter Template that can help you develop a Recipe application much faster.

Hungry is a Free Flutter Recipe App Starter Template that can help you develop a Recipe application much faster. You just need to add some adjustment to the frontend and you can create your own backend.

Jul 21, 2022
Create flutter project with all needed configuration in two minutes (theme, localization, connect to firebase, FCM, local notifications, safe API call, error handling, animation..etc)
Create flutter project with all needed configuration in two minutes (theme, localization, connect to firebase, FCM, local notifications, safe API call, error handling, animation..etc)

Flutter GetX Template Flutter Getx template to make starting project fast and easy . Introduction We all face the same problem when we want to start a

Aug 10, 2022
Api Call Check flutter - A new Flutter project that demonstrates api calling and displays them in a scrollable list
Api Call Check flutter - A new Flutter project that demonstrates api calling and displays them in a scrollable list

api_fetch A new Flutter project that demonstrates api calling and displays them

Jan 2, 2022
GetX Architecture for large scale project, This project include - pagination, pull to refresh, localization, network call and advance error handling

GetX Architecture for large scale project, This project include - pagination, pull to refresh, localization, network call and advance error handling

Jun 10, 2022
My collection of bricks created by me to help you build Flutter projects faster I think.
My collection of bricks created by me to help you build Flutter projects faster I think.

My collection of bricks created by me to help you build Flutter projects faster I think. ?? Bricks Brick Description Version presentation_layer A bric

Aug 3, 2022
My collection of bricks to help you build projects faster or nothing else will 😆
My collection of bricks to help you build projects faster or nothing else will 😆

My collection of bricks to help you build projects faster or nothing else will ?? Bricks ?? Brick Description Version annoying_analysis_options A bric

Aug 3, 2022
App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and less verbose.

App HTTP Client App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and le

Aug 4, 2022
Flutter Dropdown Alert help to notify to user when success, warning or error like push notification
Flutter Dropdown Alert help to notify to user when success, warning or error like push notification

flutter_dropdown_alert A dropdown alert package for flutter Dropdown alert will help to notify to user when you call api success, error or something l

Mar 27, 2022
Academic master is E-learning app where students can share their doubts wiith their peers they can chat and also they can find their notes
Academic master is E-learning app where students can share their doubts wiith their peers they can chat and also they can find their notes

Academic Master is E-learning App. Features:- 1) You can post real Post query in Images and video formates. 2) We will Provide notes,books and previou

Jun 30, 2022