DEV Community

loading...
Cover image for Flutter essentials: get started with great libraries

Flutter essentials: get started with great libraries

theotherdevs profile image TheOtherDev/s ・3 min read

Flutter is great! You can really do some amazing things in no time. It's just a matter of getting the hang of it. So, the first few times you'll probably need a bit of hand to achieve your goals. But don't worry, I'm sure you can do it! Your best source of knowledge here is obviously pub.dev, the great cauldron of Flutter libraries. In this article, I've tried to gather some of the greatest and most useful libraries that you'll surely find a need for.

Let's see what they are!

http

This is not only useful, it's necessary if you want to do some HTTP requests in your app (even the official Flutter website suggest to use it!). It's really simple to use and comes with great options. Here's an example:

var url = 'https://example.com/whatsit/create';
var response = await http.post(
       url, 
       body: {'name': 'doodle', 'color': 'blue'}
);

print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
print(await http.read('https://example.com/foobar.txt'));
Enter fullscreen mode Exit fullscreen mode

You can find it here.

path_provider

This is absolutely mandatory if you need to search and copy something on the file system. It's also a nice way to find commonly used locations on filesystem. This package also removes lots of boilerplate code that you should write on native platforms:

Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;

Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
Enter fullscreen mode Exit fullscreen mode

Discover more about it here

shared_preferences

What about NSUserDefaults and SharedPreferences? Great tools yeah, but on Flutter, you only need to use the shared_preferences package to access both as if by magic!

SharedPreferences prefs = await SharedPreferences.getInstance();
Enter fullscreen mode Exit fullscreen mode

Simple as that. Discover more here.

cached_network_image

Flutter has a nice way to show images from the web, but sometimes you wish for something more... efficient. This package not only shows images but also caches them. So, they're not downloaded every time you need to show them if you need to do it multiple times. For lists, this is absolutely awesome!

CachedNetworkImage(
        imageUrl: "http://via.placeholder.com/350x150",
        placeholder: (context, url) => CircularProgressIndicator(),
        errorWidget: (context, url, error) => Icon(Icons.error),
     ),
Enter fullscreen mode Exit fullscreen mode

Need a link to read about it? Here it is!

url_launcher

This package lets you launch URLs on any platform easily. It uses the common HTTP schemes to identify if it's a website URL, a phone number, or other things.

_launchURL() async {
  const url = 'https://flutter.dev';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}
Enter fullscreen mode Exit fullscreen mode

As for the app-handled URLs, it's all up to the OS. Anyway, it will feel absolutely native.

You can find this package here.

permission_handler

What's one of the most boring parts of programming? Asking for runtime permissions, right? Well, on Flutter you're pretty much obliged to use this package, but it'll make your life easier. Indeed, it provides you with some easy methods to obtain permissions and know whether certain permissions are given or not:

var status = await Permission.camera.status;
if (status.isUndetermined) {
  // We didn't ask for permission yet.
}

// You can can also directly ask the permission about its status.
if (await Permission.location.isRestricted) {
  // The OS restricts access, for example because of parental controls.
}
Enter fullscreen mode Exit fullscreen mode

Just remember also to add them to the Manifest file on Android...

You can find more about this package here.

provider

Provider is the recommended way to do State Management for apps of all sizes.
Chris Sells – Product Manager, Flutter.
June 19, 2019

The provider package is a State Management helper, which allows you to split the UI of your app from the business logic.

You can find more about this package here and also watch this video.

As you can see, there are lots of packages for Flutter! While some are a bit rough around the edges and others are way more advanced, if you need something, pub.dev probably has it. Can't find what you need? Then, make a package yourself, dude, and get famous!! (but please, keep it updated...)!

Discussion (0)

Forem Open with the Forem app