Error: The method 'ancestorStateOfType' isn't defined for the class 'BuildContext'
Asked Answered
J

7

22

When using the class Bloc provider as follows, I get the error:

'The method 'ancestorInheritedElementForWidgetOfExactType' isn't defined for the type 'BuildContext'.'

So I replaced this line context.ancestorInheritedElementForWidgetOfExactType(type)?.widget;

with this line context.getElementForInheritedWidgetOfExactType<_BlocProviderInherited<T>>().widget;

but then I get the following Error:

Error output from Xcode build:
↳
** BUILD FAILED **

Xcode's output:
↳
../../../development/flutter/.pub-cache/hosted/pub.dartlang.org/dynamic_theme-1.0.1/lib/dynamic_theme.dart:25:20: Error: The method 'ancestorStateOfType' isn't defined for the class 'BuildContext'.
 - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('../../../development/flutter/packages/flutter/lib/src/widgets/framework.dart').
Try correcting the name to the name of an existing method, or defining a method named 'ancestorStateOfType'.
    return context.ancestorStateOfType(const TypeMatcher<DynamicThemeState>());
                   ^^^^^^^^^^^^^^^^^^^

Command PhaseScriptExecution failed with a nonzero exit code
note: Using new build system
note: Building targets in parallel
note: Planning build
note: Constructing build description

Could not build the precompiled application for the device.

Error launching application on iPhone 

This is the Bloc Provider i am using:

class BlocProvider<T extends BlocBase> extends StatefulWidget {
   BlocProvider({
   Key key,
   @required this.child,
   @required this.bloc,}) : super(key: key);
   final Widget child;
   final T bloc;
   @override
   _BlocProviderState<T> createState() => _BlocProviderState<T>();

   static T of<T extends BlocBase>(BuildContext context) {
   final type = _typeOf<_BlocProviderInherited<T>>();
   _BlocProviderInherited<T> provider =
   context.ancestorInheritedElementForWidgetOfExactType(type)?.widget;
   return provider?.bloc;
  }
}

I am on the Master channel, Flutter (Channel master, 1.26.0-2.0.pre.275)

Jassy answered 10/1, 2021 at 19:44 Comment(2)
If I remember correctly, your initial error means that it does not find the bloc instance in the upstream widget treeSteviestevy
On Flutter dev channel it is working. Flutter (Channel dev, 1.26.0-1.0.pre). But unfortunately other things are not working on the dev channel.Jassy
M
27

after upgrade dev channel was almost the same issue

.pub-cache/hosted/pub.dartlang.org/dynamic_theme-1.0.1/lib/dynamic_theme.dart:25:20: Error: The method 'ancestorStateOfType' isn't defined for the class 'BuildContext'.
'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('/home/liudmila/snap/flutter/common/flutter/packages/flutter/lib/src/widgets/framework.dart').
Try correcting the name to the name of an existing method, or defining a method named 'ancestorStateOfType'.

with block 4.0.0

just change this in file .pub-cache/../dynamic_theme.dart:25:20

context.ancestorStateOfType()

on that

context.findAncestorStateOfType<DynamicThemeState>();

thats temp solution, but app is work

Marjoriemarjory answered 27/1, 2021 at 12:27 Comment(2)
I was able to fix my projectOverrule
Thanks, this workaround saved my life until we can update this dependencyZincate
C
3

You are using an old version of flutter_bloc, first you should upgrade it.

But, in fact the real problem is that context.ancestorInheritedElementForWidgetOfExactType(type) is deprecated in favor of context.getElementForInheritedWidgetOfExactType<T>() but still available in the stable channel, but in master is already removed.

Then it is necessary to update your code because Bloc has suffered breaking changes.

UPDATE: The mentioned change is already in the stable channel.

Curie answered 10/1, 2021 at 23:32 Comment(2)
I did update flutter_bloc to ^6.1.1 and did also try the pre release ^7.0.0-nullsafety.0, but none of them are working on the Master channel. Is there a new implementation for the bloc_provider? For me it seems like the Method 'ancestorStateOfType' is not available in the BuildContext class.Jassy
the context.ancestorStateOfType() is also deprecatedCurie
N
3

try changing the packages version in pubspec.yaml it worked for me i changed provider version to 5.0.0 , then it started working

Neglect answered 8/7, 2021 at 16:20 Comment(1)
Please add some code snippets to your answer to be more clear.Skeens
P
0

This error is caused when the package gets outdated. So upgrading the package is one solution to it. But sometimes, even upgrading the package does not work because maybe the creator of the package has not upgraded it or has abandoned it. In that case switch to other package.

In this particular case upgrading flutter_bloc might work.

Another similar problem i faced with GradientAppBar. It gives same error as above. The solution is to use AppBar beacuse AppBar now supports gradient inside it.

AppBar(
      flexibleSpace: Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [
          Colors.amber[700],
          Colors.grey[800],
        ]
      )
    )
  ),

I am posting about gradient app bar here because I came in this post to find the solution about GradientAppBar but couldn't find.

Parallelepiped answered 12/4, 2021 at 13:37 Comment(0)
C
0

change it from

context.findAncestorStateOfType<DynamicThemeState>();

to

context.findAncestorStateOfType<*Your_Library*>();

this will work

Crackpot answered 7/7, 2021 at 12:24 Comment(0)
G
0
context.findAncestorStateOfType<your_library_name>()

In my case this error occurs in animated_circular_chart so, I've added this line to my animated_circular_chart.dart

Gagliardi answered 9/6, 2022 at 4:53 Comment(0)
T
-1

Just change it to this it works.

context.findAncestorStateOfType<DynamicThemeState>();

it is just code update in new flutter version, older one is depreciated

Threnody answered 8/3, 2021 at 22:44 Comment(3)
Please add some explanation to your answerFulkerson
it is just code update in new flutter version older one is depreciated.Threnody
So add it into your aswerFulkerson

© 2022 - 2024 — McMap. All rights reserved.