简体   繁体   English

如何使用 FLutter Engish_Words package 中的此代码?

[英]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.

相关问题 在 Flutter 中,如何在 github 存储库中使用来自 package 的字体? - In Flutter, how do I use a font from a package in a github repo? 如何在自定义操作中使用振幅的 flutter package? - how do I use amplitude's flutter package in a custom action? 使用 flutter 打印 package,如何从 ZE6B391A8D2C4D45902A23A8B68Z 打印 PDF? - With flutter printing package, how do I print a PDF from a URL? 如何使用 Flutter MethodChannel 从本机 swift 代码调用 dart 代码中的方法? - How do I use a Flutter MethodChannel to invoke a method in dart code from the native swift code? 如何使用正确的包从颤振中的图像中读取二维码? - How can I read a QR Code from a image in flutter using proper package? 如何在 flutter 中使用来自第三方 package 资产的图像? - How can I use an image from third-party package asset in flutter? 如何从 flutter_datetime_picker package 中删除 Hour 字段? - How do I remove the Hour field from the flutter_datetime_picker package? 如何在 iOS Podfile 中指定 flutter-ffmpeg 包? - How do I specify the flutter-ffmpeg package in iOS Podfile? flutter package如何获取Android字符串资源? - How do I get Android string resources for flutter package? 如何在Flutter中将FutureIndicator与FutureBuilder一起使用? - How Do I Use RefreshIndicator with a FutureBuilder in Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM