我有带有文件夹名称列表的 showModalBottomSheet。如果用户想将文件添加到文件夹中,请单击添加,ModalBottomSheet 将显示,用户点击文件夹名称并调用 bloc 事件将文件添加到文件夹中。如果成功,我将获得 CreateFavouriteSuccess 状态,如果成功,则关闭 Modal 并显示小吃店如下。到目前为止还可以。问题是,如果用户单击快餐栏操作,我会收到以下错误。Snackbar 将在 3 秒内自动隐藏,并且 Ok 操作会立即关闭。
我了解 Navigator.of(context).pop(); 关闭祖先,shwoModalBottomSheet。我可以知道如何解决这个问题。
错误
FlutterError (Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable.
To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.)
代码
Widget build(BuildContext context) {
return BlocListener<FavouriteBloc, FavouriteState>(
listener: (context, state) {
if (state is FavouriteError) {
isSubmitButtonActive = false;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.red,
content: AutoSizeText(state.error),
duration: const Duration(seconds: 3),
action: SnackBarAction(
label: kYes,
onPressed: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
),
),
);
} else if (state is Processing) {
setState(() {
isSubmitButtonActive = false;
});
} else if (state is CreateFavouriteSuccess) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: AutoSizeText(state.successMessage),
duration: const Duration(seconds: 3),
action: SnackBarAction(
label: kYes,
onPressed: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
),
),
);
Navigator.of(context).pop();
} else if (state is CreateDownloadSuccess) {
Navigator.of(context).pop();
}
},
child: _favouriteForm(widget.socialMode),
);
}
Widget _favouriteForm(SocialMode socialMode) {
return FormBuilder(
key: formKey,
autovalidateMode: AutovalidateMode.disabled,
child: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(
height: 20,
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.9,
child: FormBuilderTextField(
textInputAction: TextInputAction.next,
name: "name",
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(context),
]),
decoration: InputDecoration(
contentPadding: const EdgeInsets.all(8),
prefixIcon: const Icon(Icons.favorite_outline),
hintText: "Enter playlist name",
hintStyle: kHintStyle,
fillColor: Colors.grey[200],
filled: true,
enabledBorder: kOutlineBorder,
focusedBorder: kOutlineBorder,
errorBorder: kOutlineErrorBorder,
focusedErrorBorder: kOutlineErrorBorder,
),
),
),
const SizedBox(
height: 20,
),
CustomButton(
onPressed: () async {
if (formKey.currentState!.validate()) {
create(socialMode, widget.currentMediaItem);
}
},
child: const Text("Create"),
)
],
),
),
),
);
}