繁体   English   中英

列表视图中的有状态小部件和重新加载会导致错误

[英]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()时,您都必须返回一个新的 state 实例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM