Flutter - Future>>关于未来建造者
下面我放了我编写的代码的简化版。 我删除了 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,
);
}
}),
),
);
}
}
我希望这是清楚的。
谢谢 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您面临的问题是您的功能
工具
正在返回future&lt; list&gt;
。list
generic(list&lt;&gt;
)未定义,则将其解释为dynamic
。这里
儿童
期望小部件的列表或换句话说 -list&lt; widget&gt;
。要解决该问题,首先将
工具
函数的返回类型更改为:然后,
outputrow
的声明为:结论:如果特定提供列表的类型是在提供列表时,您应该声明列表的通用列表以匹配所请求的列表。
The problem you are facing is that your function
tool
is returning aFuture<List>
. When theList
generic (List<>
) is not defined it is interpreted asdynamic
.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:Then the declaration of the
OutputRow
to: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.