ica*_*edo 3 dart dart-async flutter
在使用在计时器的帮助下自我更新的有状态小部件时,我一直面临一些与 setState 函数相关的问题。下面的代码显示了 2 个主要类,它们复制了我是如何发现此错误的。文本小部件“Lorem”应在 10 秒内插入 - 确实如此 - 但它从未显示。我尝试调试数组“Items”,它确实在 5 秒后包含“lorem”文本小部件,它应该如此。“构建”函数运行但在 UI 中没有任何区别。
class textList extends StatefulWidget {
@override
State<StatefulWidget> createState() =>
new _textListState();
}
class _textListState extends State<textList>
with TickerProviderStateMixin {
List<Widget> items = new List();
Widget lorem = new textClass("Lorem");
Timer timer;
@override
void initState() {
super.initState();
items.add(new textClass("test"));
items.add(new textClass("test"));
timer = new Timer.periodic(new Duration(seconds: 5), (Timer timer) {
setState(() {
items.removeAt(0);
items.add(lorem);
});
});
}
@override
void dispose() {
super.dispose();
timer.cancel();
}
@override
Widget build(BuildContext context) {
Iterable<Widget> content = ListTile.divideTiles(
context: context, tiles: items).toList();
return new Column(
children: content,
);
}
}
class textClass extends StatefulWidget {
textClass(this.word);
final String word;
@override
State<StatefulWidget> createState() =>
new _textClass(word);
}
class _textClass extends State<textClass>
with TickerProviderStateMixin {
_textClass(this.word);
String word;
Timer timer;
@override
void initState() {
super.initState();
timer = new Timer.periodic(new Duration(seconds: 2), (Timer timer) {
setState(() {
word += "t";
});
});
}
@override
void dispose() {
super.dispose();
timer.cancel();
}
@override
Widget build(BuildContext context) {
return new Text(word);
}
}
Run Code Online (Sandbox Code Playgroud)
这不是我发现此错误的方式,但这是复制它的最简单方法。主要思想是:子文本应该不断更新自己(在这种情况下,在最后添加“t”),并且在 5 秒后,最后的文本应该被替换为文本小部件“Lorem”,会发生什么在列表中,但不在 UI 中。
这是错误的:
State永远不应该有任何构造函数参数。使用该widget属性访问关联的StatefulWidget._textClass实例,因为类名和键匹配。这是一个问题,因为您只是进行了设置widget.word,initState因此您没有获取新的word配置信息。您可以通过为StatefulWidget实例提供唯一键来消除它们的歧义并导致旧State的被处理来解决这个问题,或者您可以保留旧的State并实现didUpdateWidget. 后一种方法如下所示。import 'dart:async';
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
home: new Scaffold(
appBar: new AppBar(title: new Text('Example App')),
body: new textList(),
),
));
}
class textList extends StatefulWidget {
@override
State<StatefulWidget> createState() =>
new _textListState();
}
class _textListState extends State<textList>
with TickerProviderStateMixin {
List<Widget> items = new List();
Widget lorem = new textClass("Lorem");
Timer timer;
@override
void initState() {
super.initState();
items.add(new textClass("test"));
items.add(new textClass("test"));
timer = new Timer.periodic(new Duration(seconds: 5), (Timer timer) {
setState(() {
items.removeAt(0);
items.add(lorem);
});
});
}
@override
void dispose() {
super.dispose();
timer.cancel();
}
@override
Widget build(BuildContext context) {
Iterable<Widget> content = ListTile.divideTiles(
context: context, tiles: items).toList();
return new Column(
children: content,
);
}
}
class textClass extends StatefulWidget {
textClass(this.word);
final String word;
@override
State<StatefulWidget> createState() =>
new _textClass();
}
class _textClass extends State<textClass>
with TickerProviderStateMixin {
_textClass();
String word;
Timer timer;
@override
void didUpdateWidget(textClass oldWidget) {
if (oldWidget.word != widget.word) {
word = widget.word;
}
super.didUpdateWidget(oldWidget);
}
@override
void initState() {
super.initState();
word = widget.word;
timer = new Timer.periodic(new Duration(seconds: 2), (Timer timer) {
setState(() {
word += "t";
});
});
}
@override
void dispose() {
super.dispose();
timer.cancel();
}
@override
Widget build(BuildContext context) {
return new Text(word);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5772 次 |
| 最近记录: |