📅  最后修改于: 2023-12-03 15:14:52.596000             🧑  作者: Mango
The english_words
package provides a collection of commonly used English words and utility functions that help generate random words, word pairs, and sentence suggestions for various purposes.
To use the english_words
package in your Dart project, you need to add it as a dependency in your pubspec.yaml
file:
dependencies:
english_words: ^4.0.0
You can then run flutter packages get
to download and install the package.
RandomWords is the class that generates a stream of random words when called. The example below shows how to use RandomWords to generate 10 random English words:
import 'package:english_words/english_words.dart';
void main() {
final randomWords = RandomWords();
for (var i = 0; i < 10; i++) {
print(randomWords.randomWord());
}
}
WordPair is a data model class that represents a pair of English words. You can use it to generate a list of random word pairs, as demonstrated below:
import 'package:english_words/english_words.dart';
void main() {
final randomWords = RandomWords();
final wordPairs = randomWords.take(5).toList(); // Generate 5 random word pairs
for (final pair in wordPairs) {
print(pair.asPascalCase); // Convert the pair to PascalCase
}
}
You can use english_words
to generate random sentence suggestions for your project. The generateWordPairs()
function returns an infinite iterable, which means you can call take()
to limit the number of word pairs generated:
import 'package:english_words/english_words.dart';
void main() {
final Iterable<WordPair> wordPairs = generateWordPairs().take(5);
final Iterable<String> suggestions = wordPairs.map((wordPair) {
return 'Try "${wordPair.asPascalCase}"';
});
for (String suggestion in suggestions) {
print(suggestion);
}
}
The code above generates 5 sentence suggestions using random word pairs, as shown below:
Try "PortsmouthCataract"
Try "CaryatidTrapping"
Try "InfieldDisagree"
Try "CivilityConcoct"
Try "PrickleImplant"
english_words
is a useful package for generating random English words, word pairs, and sentence suggestions. It's easy to use and can be a great tool in your project's toolkit.