I have showModalBottomSheet with list of folder name. If user want to add file into folder, click add and ModalBottomSheet will show up and user tap the folder name and call the bloc event to add file into folder. If success, I got CreateFavouriteSuccess state and if success, close the Modal and show the snack bar as follow. So far ok. The issue is that if user click snackbar action, i got the following error. Snackbar will auto hide in 3 second and Ok action to close immediately.
I understand that Navigator.of(context).pop(); close the ancestor, shwoModalBottomSheet. May I know how to solve this issue.
Error
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.)
Code
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"),
)
],
),
),
),
);
}