下面以依赖english_words包为例
1.pubspec 文件为Flutter app 管理资产和依赖,所以首先要在pubspec.yaml中添加 english_words (3.1.0 or higher) :
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
english_words: ^3.1.0
2.执行Package get,把包下载到当前项目。
$ flutter packages get
Running "flutter packages get" in startup_namer...
Process finished with exit code 0
此时会自动在pubspec.lock文件中列出已经下载到本项目的包和他们的版本等信息。
3.在lib/main.dart中导入包:
import 'package:english_words/english_words.dart';
4.在main.dart中使用它:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final wordPair = WordPair.random();
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
title: Text('Welcome to Flutter'),
),
body: Center(
child: Text(wordPair.asPascalCase),
),
)
}
}
5.选择设备,运行。
flutter device
flutter run