错误:没有为类“BuildContext”定义方法“ancestorInheritedElementForWidgetOfExactType”

Posted

技术标签:

【中文标题】错误:没有为类“BuildContext”定义方法“ancestorInheritedElementForWidgetOfExactType”【英文标题】:Error: The method 'ancestorInheritedElementForWidgetOfExactType' isn't defined for the class 'BuildContext' 【发布时间】:2021-07-26 06:07:16 【问题描述】:

我是 Flutter 的初学者,一直坚持这一点,请帮助。

错误如下所述

插件项目:firebase_core_web 未找到。请更新 settings.gradle。

插件项目:firebase_auth_web 未找到。请更新 settings.gradle。

插件项目:cloud_firestore_web 未找到。请更新 settings.gradle。

/C:/Flutter/.pub-cache/hosted/pub.dartlang.org/provider-2.0.1+1/lib/src/provider.dart:179:19:错误:方法“inheritFromWidgetOfExactType”是'没有为类'BuildContext'定义。 -'BuildContext' 来自'package:flutter/src/widgets/framework.dart' ('/C:/Flutter/packages/flutter/lib/src/widgets/framework.dart')。 尝试将名称更正为现有方法的名称,或定义名为“inheritFromWidgetOfExactType”的方法。 ? context.inheritFromWidgetOfExactType(type) as _Provider

              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

/C:/Flutter/.pub-cache/hosted/pub.dartlang.org/provider-2.0.1+1/lib/src/provider.dart:180:19:错误:方法“ancestorInheritedElementForWidgetOfExactType”是'没有为类'BuildContext'定义。 -'BuildContext' 来自'package:flutter/src/widgets/framework.dart' ('/C:/Flutter/packages/flutter/lib/src/widgets/framework.dart')。 尝试将名称更正为现有方法的名称,或定义名为“ancestorInheritedElementForWidgetOfExactType”的方法。 : context.ancestorInheritedElementForWidgetOfExactType(type)?.widget ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

或者请告诉我在下面这张图片中写什么来代替第 179,180 行 image description here

【问题讨论】:

【参考方案1】:

像这样去 android 文件夹更新你的 buildgradle 文件:

buildscript 
    repositories 
        google()
        jcenter()
    

    dependencies 
        classpath 'com.android.tools.build:gradle:3.5.0'
        classpath 'com.google.gms:google-services:4.3.3'
    


allprojects 
    repositories 
        google()
        jcenter()
    


rootProject.buildDir = '../build'
subprojects 
    project.buildDir = "$rootProject.buildDir/$project.name"

subprojects 
    project.evaluationDependsOn(':app')


task clean(type: Delete) 
    delete rootProject.buildDir

然后转到Android->App->src->build gradle 检查应用插件是否存在。

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) 
    localPropertiesFile.withReader('UTF-8')  reader ->
        localProperties.load(reader)
    


def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) 
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")


def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) 
    flutterVersionCode = '1'


def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) 
    flutterVersionName = '1.0'


apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

   def keystoreProperties = new Properties()
   def keystorePropertiesFile = rootProject.file('key.properties')
   if (keystorePropertiesFile.exists()) 
       keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
   

   android 
    compileSdkVersion 28

    lintOptions 
        disable 'InvalidPackage'
    

    defaultConfig 
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.asd.emediadesk"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    

   signingConfigs 
       release 
           keyAlias keystoreProperties['keyAlias']
           keyPassword keystoreProperties['keyPassword']
           storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
           storePassword keystoreProperties['storePassword']
       
   
   buildTypes 
       release 
           signingConfig signingConfigs.release
       
   


flutter 
    source '../..'


dependencies 
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'


apply plugin: 'com.google.gms.google-services'

【讨论】:

感谢您的宝贵时间,我按照您说的做了,但没有成功,但我更担心这个错误:'ancestorInheritedElementForWidgetOfExactType' 没有为类 'BuildContext 定义',因为这个问题也发生在我的不同项目中。请检查我最后附上的图片。 在 main.dart 检查您是否正确分配了提供程序和材料应用程序?

以上是关于错误:没有为类“BuildContext”定义方法“ancestorInheritedElementForWidgetOfExactType”的主要内容,如果未能解决你的问题,请参考以下文章

Flushbar 插件:错误:没有为类“FocusScopeNode”定义方法“附加”

Flutter 2 构建项目:错误:没有为类“_LoadingIndicatorState”定义方法“SpinKitPouringHourglass”

没有为类“对象”定义运算符“[]”。扑

没有为类'Person'定义方法'set_age'。飞镖

没有为类“BasicMessageChannel<dynamic>”定义方法“setMockMessageHandler”

错误:没有为类 'Utf8' 定义 getter 'addressOf'