Flutter Network Helper method for http package
I have been learning and working on flutter for the last one year. I come from a Javascript and Web development background. You know how Javascript is a loosely typed language and how Dart is a strictly typed language. It’s always a headache for Javascript developers to write strictly typed languages like dart.
So while learning dart/flutter it became a habit for me to modularize code as I learn/develop, so it can be used in other projects without getting too much into the syntax.
One such effort was to write a helper function to make it easy to use http package while adding the functionality of working with live data in flutter. Here is the code that I wrote and I’m sharing it here with the intension that it would help a lot of new/experienced dart/flutter developers. Please leave feedback if you think we can improve this more.
Thanks
Here is the github link from where you can download this code:
https://github.com/asifmai/dart-flutter-modules/blob/master/network.dart
Usage
- Import “network.dart” inside the widget/provider where you want to use it.
- All the request methods, i.e. GET, POST, PUT, DELETE, PATCH are supported.
3. To make a get request simply do this:
await apiRequest(url: “https://apiendpoint.com”, method: RequestMethod.GET);
4. To make a post request simply do this:
await apiRequest(url: “https://apiendpoint.com”, method: RequestMethod.GET, data: {location: “New York”});
and so on…