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

Overview

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

You might also like...

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

Nov 15, 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 20, 2022

Call Kit is a prebuilt feature-rich call component, which enables you to build one-on-one and group voice/video calls into your app with only a few lines of code.

Call Kit is a prebuilt feature-rich call component, which enables you to build one-on-one and group voice/video calls into your app with only a few lines of code.

Call Kit (ZegoUIKitPrebuiltCall) Call Kit is a prebuilt feature-rich call component, which enables you to build one-on-one and group voice/video calls

Dec 26, 2022

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 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

Dec 17, 2022

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

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

Dec 19, 2022
Owner
Abdelrahman Saed
Software developer with professional experience focused in Flutter, previous experience with Android native(java), and familiar with backend using NodeJs.
Abdelrahman Saed
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

Muhammad Rezky Sulihin 54 Nov 26, 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. You just need to add some adjustment to the frontend and you can create your own backend.

Muhammad Rezky Sulihin 48 Dec 20, 2022
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

Babish Shrestha 0 Jan 2, 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)

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

Emad Beltaje 150 Jan 7, 2023
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

Wai Han Ko 5 Nov 29, 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. Features:- 1) You can post real Post query in Images and video formates. 2) We will Provide notes,books and previou

amit singh 25 Dec 14, 2022
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

Md Abir Ahsan Tahmim 1 Dec 8, 2021
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

Joanna May 44 Nov 1, 2022
A fast start flutter project to make aps faster and skip setup on every application. I am personally using this structure while creating a new project

Flutter Fast Start A fast start flutter project to make apps faster and skip setup on every application. I am personally using this structure while cr

Okan Demir 2 Dec 15, 2022