How to use external packages in Flutter

Building a quote app Using Quote external package.

·

2 min read

How to use external packages in Flutter

An external package allows us to quickly achieve the functionality we need without writing code from scratch. In this example, I am using use an open-source package named quotes , which contains top 500 English quotes as of now.

You can find the quotes package, as well as many other open source packages, on pub.dev .

Step-1. The pubspec.yaml file manages the assets and dependencies for a Flutter app. In pubspec.yaml, add quotes to the dependencies list:

dependencies:
  flutter:
    sdk: flutter
    quotes:

Step-2. While viewing the pubspec.yaml file in Android Studio’s editor view, click Pub get. This pulls the package into your project. You should see the following in the console:

wxttt.png

Step-3. In lib/main.dart, import the new package:

import 'package:quotes/quotes.dart';

As you type, Android Studio gives you suggestions for libraries to import. It then renders the import string in gray, letting you know that the imported library is unused (so far).

Step-4. Use the Quotes package to generate new quotes every time the app is launched.

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final quotes = Quotes.getFirst().getContent();
    return MaterialApp(
      title: 'Quotes',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Quotes'),
        ),
        body: Center(
          child: Text(quotes),
        ),
      ),
    );
  }
}

Step-5. Run your App on AVD or any physical device

Step-6. If the app is running, hot reload to update the running app. Each time you click hot reload, or save the project, you should see a different Quote, chosen at random, in the running app