request_state 0.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • new50

request_state #

An alternative to flutter's built-in State class made specifically to deal with async http requests.

Getting Started #

Add the package to your pubspec.yaml:

dependencies:
  request_state: ^0.0.1

Using the package #

Using RequestState is really simple. Assume you have a StatefulWidget called SomeWidget that you want to work with http requests.

import 'package:flutter/material.dart';

class SomeWidget extends StatefulWidget {
  @override
  _SomeWidgetState createState() => _SomeWidgetState();
}

class _SomeWidgetState extends State<SomeWidget> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

To allow this widget to use RequestState you:

  1. Import RequestState:
import 'package:request_state/request_state.dart';
  1. Exchange State<SomeWidget> for RequestState<SomeWidget>
import 'package:flutter/material.dart';
import 'package:request_state/request_state.dart';

class SomeWidget extends StatefulWidget {
  @override
  _SomeWidgetState createState() => _SomeWidgetState();
}

class _SomeWidgetState extends RequestState<SomeWidget> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Then, you can remove the build() method override, and instead you must override idleBody(), waitingForResponseBody() and responseReceivedBody():

import 'package:flutter/material.dart';
import 'package:request_state/request_state.dart';

class SomeWidget extends StatefulWidget {
  @override
  _SomeWidgetState createState() => _SomeWidgetState();
}

class _SomeWidgetState extends RequestState<SomeWidget> {
  @override
  Widget idleBody(BuildContext context) {
    return Container();
  }
  
  @override
  Widget waitingForResponseBody(BuildContext context) {
    return CircularProgressIndicator();
  }
  
  @override
  Widget receivedResponseBody(BuildContext context) {
    return Text("Response Received");
  }
}

These three methods define what child your widget should show at different stages of the request. idleBody() is shown before a request has been sent for the first time, waitingForResponseBody() is shown once a request has been sent but no response has been received yet, and receivedResponseBody() is shown when a response has successfully been received.

You may also optionally override requestFailedBody() to define what is shown when sending the request fails. By default this uses the widget provided to idleBody().

import 'package:flutter/material.dart';
import 'package:request_state/request_state.dart';

class SomeWidget extends StatefulWidget {
  @override
  _SomeWidgetState createState() => _SomeWidgetState();
}

class _SomeWidgetState extends RequestState<SomeWidget> {
  @override
  Widget idleBody(BuildContext context) {
    return Container();
  }
  
  @override
  Widget waitingForResponseBody(BuildContext context) {
    return CircularProgressIndicator();
  }
  
  @override
  Widget receivedResponseBody(BuildContext context) {
    return Text("Response Received");
  }
  
  @override
  Widget requestFailedBody(BuildContext context) {
    return Text("Sending Request Failed");
  }
}

All four of these methods work the way you are used to with normal State's build() method.

Sending a request #

To send a request you use the build-in sendRequest() method, passing in the URL to send the request to:

@override
Widget idleBody(BuildContext context) {
  return RaisedButton(
    child: Text("Send Request"),
    onPressed: () {
      sendRequest("https://my-api-url.com");
    }
  );
}

This will send a GET request to https://my-api-url.com, showing waitingForResponseBody() whilst waiting for a response from the API, and then show receivedResponse()Body when a response is received. Alternatively, if sending the request fails, it will show the value of requestFailedBody().

You may optionally also specify the type of the request (only GET or POST supported as of version 0.0.1) by passing a value of the RequestType enum to named type parameter (GET by default), a body for the request, a Map<String, dynamic> passed to the named body parameter.

If you would like to fire a callback when a response is successfully received, you may pass a callback to the onReceived argument of the sendRequest() method. The callback should have a void return type and provides you with a Response object (from the http package) containing the received response.

@override
Widget idleBody(BuildContext context) {
  return RaisedButton(
    child: Text("Send Request"),
    onPressed: () {
      sendRequest("https://my-api-url.com", onReceived: (response) {
        print("Response successfully received. The body of the response is: ${response.body}");
      });
    }
  );
}

Similarly, if you would like to fire a callback when sending a request is unsuccessful, you may pass it to the onError argument of the sendRequest() method. Like onReceived, this should also have a void return type, and provides you with the Exception object that was thrown.

@override
Widget idleBody(BuildContext context) {
  return RaisedButton(
    child: Text("Send Request"),
    onPressed: () {
      sendRequest("https://my-api-url.com", onError: (error) {
        print("Sending request failed. The error is: $error");
      });
    }
  );
}

Contributing #

If you encounter a bug or have a suggestion, please open an issue for it on GitHub. Feel free to fork the repo and submit PRs on the original one. All suggestions and contributions are welcome.

[0.1.0] - 21/11/2019

  • Format request_state.dart with dart formatter

[0.0.1+1] - 21/11/2019

  • Fix missing description in pubspec.yaml

[0.0.1] - 21/11/2019

Initial Release:

  • RequestState class
  • sendRequest() method
    • get
    • post
  • Initial ReadMe file detailing features from 0.0.1
  • Changelog
  • License

example/README.md

example #

A new Flutter project.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  request_state: ^0.1.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:request_state/request_state.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Dec 13, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.13.1+4
  • Flutter: 1.12.13+hotfix.2

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
dio ^3.0.5 3.0.7
flutter 0.0.0
Transitive dependencies
charcode 1.1.2
collection 1.14.11 1.14.12
http_parser 3.1.3
meta 1.1.8
path 1.6.4
sky_engine 0.0.99
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test