I'm using the Image Picker plugin to pick an image. I would like to navigate to a new screen immediately after an image is picked but it's not working. I'm getting an error that states that the context doesn't exist in the current widget tree.
Below is my code.
pickImage(BuildContext context) async {
File pickedImage = await ImagePicker.pickImage(source: ImageSource.camera);
if (pickedImage != null) {
print(pickedImage.path);
if (this.mounted) {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ViewStory(
localImagePath: pickedImage.path,
),
),
);
}
}
}
Call the function like this:
IconButton(
onPressed: () => pickImage(context),
icon: Icon(
Icons.camera_alt,
color: CustomColors.primary,
size: 100,
),
),
Below is the error I'm getting:
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 inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.)