http_solver 0.1.2

Flutter Android iOS

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

http_solver #


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

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

Post to api #

getFromApi static method take YourModel and your Api url and return Future

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

convert model to either #

toEither() extension on Future

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 #

9
likes
80
pub points
7%
popularity

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

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

abdoo.dev@gmail.com

License

MIT (LICENSE)

Dependencies

connectivity, dartz, flutter, http

More

Packages that depend on http_solver