Home > Back-end >  error with PhotoView : Looking up a deactivated widget's ancestor is unsafe
error with PhotoView : Looking up a deactivated widget's ancestor is unsafe

Time:12-19

I have read few stackoverflow posts about "Looking up a deactivated widget's ancestor is unsafe" error but couldn't find an answer which work.

I've tried to set a global key with the scaffold, and to use WidgetsBinding.instance.addPostFrameCallback() without success.

I'm pretty sure I'm doing something stupid and easy to fix, but I can't figure out what.

This is a simple version of the code which replicates the error when you go back from PhotoViewPage (photo_view package) :

my_home_page.dart

import 'package:flutter/material.dart';
import 'package:phototest/photo_view_page.dart';


class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextButton(
      child: const Text("to PhotoView"),
      onPressed: () => _toPhotoView(context),
    );
  }

  void _toPhotoView(BuildContext context) {
    Navigator.of(context).push(
      MaterialPageRoute<dynamic>(
        builder: (BuildContext context) => const PhotoViewPage(),
      ),
    );
  }
}

photo_view_page.dart

import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';

class PhotoViewPage extends StatelessWidget {
  const PhotoViewPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PhotoView(imageProvider: AssetImage("assets/image.png"));
  }
}

CodePudding user response:

I'm not sure where the error was from, but switching flutter channel from master to stable fixed it.

flutter channel stable

flutter upgrade --force

CodePudding user response:

It seems like you need to put your PhotoView inside another Widget:

@override
Widget build(BuildContext context) {
  return Container(
    child: PhotoView(
      imageProvider: AssetImage("assets/image.png"),
    )
  );
}

Have a look at the minimal example from the official docs. They are not returning PhotoView directly either.

  • Related