[英]How do I use this code from the FLutter Engish_Words package?
I'm trying to learn Flutter (As someone without a lot of programming background) and in a tutorial have just learned how to import the English Words package.我正在尝试学习 Flutter(作为没有很多编程背景的人)并且在教程中刚刚学会了如何导入英文单词 package。
I have tried to use the example code as shown at: https://pub.dev/packages/english_words我尝试使用示例代码,如下所示: https://pub.dev/packages/english_words
The error I receive is: This expression has a type of void so it's value cannot be used.我收到的错误是:此表达式的类型为 void,因此无法使用它的值。 It is the line: nouns.take(50).forEach(print);
这是行:nouns.take(50).forEach(print); that gives the error.
这给出了错误。
This is the code:这是代码:
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
void main() {
home: Scaffold(
body: SafeArea(
child: Center(
child: Container(
child: Text(
nouns.take(50).forEach(print);
),
),
),
),
);
}
Thanks for any clues.感谢您提供任何线索。
The error occurs because nouns
returns a List<String>
and foreach
is a void
method, which returns nothing.发生错误是因为
nouns
返回List<String>
并且foreach
是一个void
方法,它不返回任何内容。 But your Text
requires a String.但是您的
Text
需要一个字符串。 If you want to display all nouns make use of ListView .如果要显示所有名词,请使用ListView 。 Below is a small example.
下面是一个小例子。
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final List<String> words = nouns.take(50).toList();
return MaterialApp(
home: Scaffold(
body: ListView.builder(
itemBuilder: (context, index) => Text(words[index]),
itemCount: words.length,
),
),
);
}
}
Everything in flutter is a Widget as well as Text , used as a child of Container widget. flutter 中的所有内容都是一个 Widget 以及Text ,用作 Container 小部件的子级。
The error I receive is: This expression has a type of void so its value cannot be used.
我收到的错误是:此表达式的类型为 void,因此无法使用其值。 It is the line: nouns.take(50).forEach(print);
这是行:nouns.take(50).forEach(print); that gives the error.
这给出了错误。
the line: nouns.take(50).forEach(print)
, actually return a list of 50 most used nouns and printing them.该行:
nouns.take(50).forEach(print)
,实际上返回 50 个最常用名词的列表并打印它们。 In Dart & Flutter print
is a function that return void.在 Dart 和 Flutter
print
是返回无效的 function。 void means nothing. void没有任何意义。 so the line
nouns.take(50).forEach(print)
actually returns nothing but Text Widget requires String
in order to display on screen.所以
nouns.take(50).forEach(print)
行实际上什么都不返回,但Text Widget需要String
才能显示在屏幕上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.