列表视图中的有状态小部件和重新加载导致错误
Stateful widget in listview and reloading leads to error
当我使用无状态小部件数组制作 ListView 时,我没有收到任何错误,一切正常。但是,当我将有状态小部件放入数组并通过滚动它重新呈现小部件以使其在视图之外结束时,我会收到错误消息。
我在ListView和ListView.builder上试过,好像没什么区别。我认为问题出在文本上,所以我尝试删除所有 Text 构造函数,但没有区别。小部件会初始化和处理但不会重新初始化。
class CustomSettingsScreen extends StatefulWidget {
static const String routeName = "/settings";
@override
CustomSettingsScreenState createState() =>CustomSettingsScreenState();
}
class CustomSettingsScreenState extends State<CustomSettingsScreen> {
@override
void initState() {
super.initState();
}
final listElements = {
Divider(height: 500),
VibrateStartRecordingEnablePreference(
preferenceKey: PreferenceKeys.keyVibrateStartRecordingEnable,
),
Divider(height: 500),
Divider(height: 500),
Divider(height: 500),
Divider(height: 500),
};
};
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Settings"),
),
body: ListView.builder(
itemCount: listElements.length,
itemBuilder: (BuildContext context, int index) {
return listElements.elementAt(index);
},
),);
}
}
class VibrateStartRecordingEnablePreference extends StatefulWidget {
VibrateStartRecordingEnablePreference({
Key key,
@required this.preferenceKey,
});
final preferenceKey;
final _VibrateStartRecordingEnablePreferenceState
_vibrateStartRecordingEnablePreferenceState =
new _VibrateStartRecordingEnablePreferenceState();
@override
_VibrateStartRecordingEnablePreferenceState createState() =>
_vibrateStartRecordingEnablePreferenceState;
}
class _VibrateStartRecordingEnablePreferenceState extends State<VibrateStartRecordingEnablePreference> {
String _title = "Vibrate at the start of recording";
String _subtitleOn = "Vibrates once upon start of recording";
String _subtitleOff = "Will not vibrate at start";
bool _value = true;
String _getSubtitle() {
if (_value)
return _subtitleOn;
else
return _subtitleOff;
}
void _updateValue(bool value) {
Settings().save(widget.preferenceKey, value);
setState(() {
_value = value;
});
}
@override
void initState() {
//Update the values and text
Settings().getBool(widget.preferenceKey, true).then((value) {
setState(() {
_value = value;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return new SwitchListTile(
secondary: const Icon(Icons.vibration),
title: Text("$_title"),
subtitle: Text(_getSubtitle()),
value: _value,
onChanged: (value) {
_updateValue(value);
},
);
}
}
调试器错误
The following assertion was thrown building NotificationListener<KeepAliveNotification>: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4006 pos
12: '_state._widget == null': is not true.
这是一段视频,展示了正在发生的事情。
https://webm.red/6ZTH
_vibrateStartRecordingEnablePreferenceState
存储在 VibrateStartRecordingEnablePreference
中,这是错误的。
框架尝试将状态实例附加到小部件,但状态已经附加到一个小部件。所以会导致异常。
每次在 StatefulWidget
中调用 createState()
时,您都必须 return 一个新的状态实例
当我使用无状态小部件数组制作 ListView 时,我没有收到任何错误,一切正常。但是,当我将有状态小部件放入数组并通过滚动它重新呈现小部件以使其在视图之外结束时,我会收到错误消息。
我在ListView和ListView.builder上试过,好像没什么区别。我认为问题出在文本上,所以我尝试删除所有 Text 构造函数,但没有区别。小部件会初始化和处理但不会重新初始化。
class CustomSettingsScreen extends StatefulWidget {
static const String routeName = "/settings";
@override
CustomSettingsScreenState createState() =>CustomSettingsScreenState();
}
class CustomSettingsScreenState extends State<CustomSettingsScreen> {
@override
void initState() {
super.initState();
}
final listElements = {
Divider(height: 500),
VibrateStartRecordingEnablePreference(
preferenceKey: PreferenceKeys.keyVibrateStartRecordingEnable,
),
Divider(height: 500),
Divider(height: 500),
Divider(height: 500),
Divider(height: 500),
};
};
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Settings"),
),
body: ListView.builder(
itemCount: listElements.length,
itemBuilder: (BuildContext context, int index) {
return listElements.elementAt(index);
},
),);
}
}
class VibrateStartRecordingEnablePreference extends StatefulWidget {
VibrateStartRecordingEnablePreference({
Key key,
@required this.preferenceKey,
});
final preferenceKey;
final _VibrateStartRecordingEnablePreferenceState
_vibrateStartRecordingEnablePreferenceState =
new _VibrateStartRecordingEnablePreferenceState();
@override
_VibrateStartRecordingEnablePreferenceState createState() =>
_vibrateStartRecordingEnablePreferenceState;
}
class _VibrateStartRecordingEnablePreferenceState extends State<VibrateStartRecordingEnablePreference> {
String _title = "Vibrate at the start of recording";
String _subtitleOn = "Vibrates once upon start of recording";
String _subtitleOff = "Will not vibrate at start";
bool _value = true;
String _getSubtitle() {
if (_value)
return _subtitleOn;
else
return _subtitleOff;
}
void _updateValue(bool value) {
Settings().save(widget.preferenceKey, value);
setState(() {
_value = value;
});
}
@override
void initState() {
//Update the values and text
Settings().getBool(widget.preferenceKey, true).then((value) {
setState(() {
_value = value;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return new SwitchListTile(
secondary: const Icon(Icons.vibration),
title: Text("$_title"),
subtitle: Text(_getSubtitle()),
value: _value,
onChanged: (value) {
_updateValue(value);
},
);
}
}
调试器错误
The following assertion was thrown building NotificationListener<KeepAliveNotification>: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4006 pos
12: '_state._widget == null': is not true.
这是一段视频,展示了正在发生的事情。 https://webm.red/6ZTH
_vibrateStartRecordingEnablePreferenceState
存储在 VibrateStartRecordingEnablePreference
中,这是错误的。
框架尝试将状态实例附加到小部件,但状态已经附加到一个小部件。所以会导致异常。
每次在 StatefulWidget
中调用createState()
时,您都必须 return 一个新的状态实例