这是怎么回事:
- A
State
绝对不能有任何构造函数参数。使用该widget
属性可以访问关联的的最终属性StatefulWidget
。 Flutter
正在重用您的_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
StatecreateState() =>
new _textListState();
}class _textListState extends State
with TickerProviderStateMixin {List
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) {
Iterablecontent = ListTile.divideTiles(
context: context, tiles: items).toList();return new Column( children: content,);
}
}class textClass extends StatefulWidget {
textClass(this.word);final String word;
@override
StatecreateState() =>
new _textClass();
}class _textClass extends State
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);
}
}



