Flutter - Future>>关于未来建造者

发布于 2025-01-20 19:45:11 字数 2668 浏览 2 评论 0 原文

下面我放了我编写的代码的简化版。 我删除了 tool() 函数的一部分,因为它对于这个问题的目的并不重要。

我希望 List OutputRow 显示为 FutureBuilder 内 Wrap() 的子级。

目前它给了我这个错误:类型“List”不是类型“List”的子类型。

有人知道我该如何修复它吗?

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

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

class mainApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Chat(),
    );
  }
}

class Chat extends StatefulWidget {
  const Chat({Key? key}) : super(key: key);

  @override
  _ChatState createState() => _ChatState();
}

class _ChatState extends State<Chat> {
  String text = 'Henlo. I am Gabriele. I am 21 yers old!';

  Future<List> tool(String text) async {
    var tool = LanguageTool();
    var result = tool.check(text);
    var correction = await result;

    // code simplification {...}

    List OutputList = [
      '',
      'Henlo',
      '. I am Gabriele. I am 21 ',
      'yers',
      ' old!'
    ];
    List OutputBool = [false, true, false, true, false];
    List OutputIndex = [0, 1, 2, 3, 4];

    List OutputRow = [];

    for (int i in OutputIndex) {
      if (OutputBool[i] == false) {
        OutputRow.add(
          Container(
            child: Text(
              OutputList[i],
              style: TextStyle(fontSize: 18.0),
            ),
          ),
        );
      } else if (OutputBool[i] == true) {
        OutputRow.add(
          Container(
            child: GestureDetector(
              onTap: () {},
              child: Container(
                color: Colors.orange,
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    OutputList[i],
                    style: TextStyle(fontSize: 18.0),
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }

    print(OutputRow);
    return OutputRow;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: FutureBuilder(
            future: tool(text),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (snapshot.data == null) {
                return Center(
                  child: Text('Loading...'),
                );
              } else {
                return Wrap(
                  crossAxisAlignment: WrapCrossAlignment.center,
                  runSpacing: 4.0,
                  children: snapshot.data,
                );
              }
            }),
      ),
    );
  }
}

我希望这是清楚的。

谢谢 :)

below I put a simplification of a code I wrote.
I have removed a part of the tool() function because it is not important for the purpose of this question.

I would like the List OutputRow to appear as children of the Wrap() inside the FutureBuilder.

Currently it gives me this error: type 'List ' is not a subtype of type 'List '.

Anyone know how I can fix it?

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

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

class mainApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Chat(),
    );
  }
}

class Chat extends StatefulWidget {
  const Chat({Key? key}) : super(key: key);

  @override
  _ChatState createState() => _ChatState();
}

class _ChatState extends State<Chat> {
  String text = 'Henlo. I am Gabriele. I am 21 yers old!';

  Future<List> tool(String text) async {
    var tool = LanguageTool();
    var result = tool.check(text);
    var correction = await result;

    // code simplification {...}

    List OutputList = [
      '',
      'Henlo',
      '. I am Gabriele. I am 21 ',
      'yers',
      ' old!'
    ];
    List OutputBool = [false, true, false, true, false];
    List OutputIndex = [0, 1, 2, 3, 4];

    List OutputRow = [];

    for (int i in OutputIndex) {
      if (OutputBool[i] == false) {
        OutputRow.add(
          Container(
            child: Text(
              OutputList[i],
              style: TextStyle(fontSize: 18.0),
            ),
          ),
        );
      } else if (OutputBool[i] == true) {
        OutputRow.add(
          Container(
            child: GestureDetector(
              onTap: () {},
              child: Container(
                color: Colors.orange,
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    OutputList[i],
                    style: TextStyle(fontSize: 18.0),
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }

    print(OutputRow);
    return OutputRow;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: FutureBuilder(
            future: tool(text),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (snapshot.data == null) {
                return Center(
                  child: Text('Loading...'),
                );
              } else {
                return Wrap(
                  crossAxisAlignment: WrapCrossAlignment.center,
                  runSpacing: 4.0,
                  children: snapshot.data,
                );
              }
            }),
      ),
    );
  }
}

I hope it is clear.

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無心 2025-01-27 19:45:11

您面临的问题是您的功能工具正在返回 future&lt; list&gt; list generic( list&lt;&gt; )未定义,则将其解释为 dynamic

return Wrap(
    crossAxisAlignment: WrapCrossAlignment.center,
    runSpacing: 4.0,
    children: snapshot.data,
);

这里儿童期望小部件的列表或换句话说 - list&lt; widget&gt;

要解决该问题,首先将工具函数的返回类型更改为:

Future<List<Widget>> tool(String text) async {

然后, outputrow 的声明为:

    List<Widget> OutputRow = [];

结论:如果特定提供列表的类型是在提供列表时,您应该声明列表的通用列表以匹配所请求的列表。

The problem you are facing is that your function tool is returning a Future<List>. When the List generic (List<>) is not defined it is interpreted as dynamic.

return Wrap(
    crossAxisAlignment: WrapCrossAlignment.center,
    runSpacing: 4.0,
    children: snapshot.data,
);

Here children expects list of widgets or in other words - List<Widget>.

To fix the issue first change the return type of your tool function to:

Future<List<Widget>> tool(String text) async {

Then the declaration of the OutputRow to:

    List<Widget> OutputRow = [];

In conclusion: If a specific type of list is expected when providing it you should declarate the generic of the list to match the requested one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文