flutter Error:未找到方法loadBannerAd的实现异常(未找到方法loadBannerAd的实现)

ljsrvy3e  于 2023-01-09  发布在  Flutter
关注(0)|答案(1)|浏览(150)

我正在尝试使用google_mobile_ads将admob集成到我的flutter应用程序中:^1.2.0插件。但它给我这个错误:

Error: MissingPluginException(No implementation found for method loadBannerAd on channel
plugins.flutter.io/google_mobile_ads)
    at Object.throw_ [as throw] (http://localhost:54890/dart_sdk.js:5067:11)
    at platform_channel.MethodChannel.new._invokeMethod
    (http://localhost:54890/packages/flutter/src/services/restoration.dart.lib.js:1560:21)
    at _invokeMethod.next (<anonymous>)
    at http://localhost:54890/dart_sdk.js:40571:33
    at _RootZone.runUnary (http://localhost:54890/dart_sdk.js:40441:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:54890/dart_sdk.js:35363:29)
    at handleValueCallback (http://localhost:54890/dart_sdk.js:35931:49)
    at Function._propagateToListeners (http://localhost:54890/dart_sdk.js:35969:17)
    at _Future.new.[_completeWithValue] (http://localhost:54890/dart_sdk.js:35817:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:54890/dart_sdk.js:35838:35)
    at Object._microtaskLoop (http://localhost:54890/dart_sdk.js:40708:13)
    at _startMicrotaskLoop (http://localhost:54890/dart_sdk.js:40714:13)
    at http://localhost:54890/dart_sdk.js:36191:9
    at Object._microtaskLoop (http://localhost:54890/dart_sdk.js:40708:13)
    at _startMicrotaskLoop (http://localhost:54890/dart_sdk.js:40714:13)
    at http://localhost:54890/dart_sdk.js:36191:9

我找不到解决这个问题的方法。我已经找了一个月的方法。我试过了所有网站上推荐的解决方法。但是没有一个人帮我。有人能帮我解决这个问题吗?下面是我的主要源代码:

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

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Future<InitializationStatus> _initGoogleMobileAds() {
    return MobileAds.instance.initialize();
  }

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool isBannerReady = false;
  late BannerAd myBanner;

  Future<void> loadAd() {
    myBanner = BannerAd(
      adUnitId: 'ca-app-pub-3940256099942544/6300978111',
      size: AdSize.banner,
      request: const AdRequest(),
      listener: BannerAdListener(
        onAdLoaded: (Ad ad) {
          isBannerReady = true;
          print('Ad loaded.');
        },
        onAdFailedToLoad: (Ad ad, LoadAdError error) {
          print('Ad failed to load: $error');
        },
        onAdOpened: (Ad ad) => print('Ad opened.'),
        onAdClosed: (Ad ad) => print('Ad closed.'),
        onAdImpression: (Ad ad) => print('Ad impression.'),
      ),
    );
    return myBanner.load();
  }

  @override
  didChangeDependencies() {
    super.didChangeDependencies();
    loadAd();
  }

  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue[100],
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        children: [
          Expanded(
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  const Text(
                    'You have pushed the button this many times:',
                  ),
                  Text(
                    '$_counter',
                    style: Theme.of(context).textTheme.headline4,
                  ),
                ],
              ),
            ),
          ),
          isBannerReady
              ? Align(
                  alignment: Alignment.topCenter,
                  child: SizedBox(
                    width: myBanner.size.width.toDouble(),
                    height: myBanner.size.height.toDouble(),
                    child: AdWidget(ad: myBanner),
                  ),
                )
              : Container(
                  height: 50,
                  color: Colors.white,
                ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}
    • 安卓清单. xml**
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testad">
   <application
        android:label="testad"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713"/>
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

有趣的是当我加上

MobileAds.instance.initialize();

在我的主方法中

Future<InitializationStatus> _initGoogleMobileAds() {
    return MobileAds.instance.initialize();
  }

错误消息将更改为

Error: MissingPluginException(No implementation found for method _init on channel
plugins.flutter.io/google_mobile_ads)
    at Object.throw_ [as throw] (http://localhost:54890/dart_sdk.js:5067:11)
    at platform_channel.MethodChannel.new._invokeMethod
    (http://localhost:54890/packages/flutter/src/services/restoration.dart.lib.js:1560:21)
    at _invokeMethod.next (<anonymous>)
    at http://localhost:54890/dart_sdk.js:40571:33
    at _RootZone.runUnary (http://localhost:54890/dart_sdk.js:40441:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:54890/dart_sdk.js:35363:29)
    at handleValueCallback (http://localhost:54890/dart_sdk.js:35931:49)
    at Function._propagateToListeners (http://localhost:54890/dart_sdk.js:35969:17)
    at _Future.new.[_completeWithValue] (http://localhost:54890/dart_sdk.js:35817:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:54890/dart_sdk.js:35838:35)
    at Object._microtaskLoop (http://localhost:54890/dart_sdk.js:40708:13)
    at _startMicrotaskLoop (http://localhost:54890/dart_sdk.js:40714:13)
    at http://localhost:54890/dart_sdk.js:36191:9
Error: MissingPluginException(No implementation found for method MobileAds#initialize on channel
plugins.flutter.io/google_mobile_ads)
    at Object.throw_ [as throw] (http://localhost:54890/dart_sdk.js:5067:11)
    at platform_channel.MethodChannel.new._invokeMethod
    (http://localhost:54890/packages/flutter/src/services/restoration.dart.lib.js:1560:21)
    at _invokeMethod.next (<anonymous>)
    at http://localhost:54890/dart_sdk.js:40571:33
    at _RootZone.runUnary (http://localhost:54890/dart_sdk.js:40441:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:54890/dart_sdk.js:35363:29)
    at handleValueCallback (http://localhost:54890/dart_sdk.js:35931:49)
    at Function._propagateToListeners (http://localhost:54890/dart_sdk.js:35969:17)
    at _Future.new.[_completeWithValue] (http://localhost:54890/dart_sdk.js:35817:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:54890/dart_sdk.js:35838:35)
    at Object._microtaskLoop (http://localhost:54890/dart_sdk.js:40708:13)
    at _startMicrotaskLoop (http://localhost:54890/dart_sdk.js:40714:13)
    at http://localhost:54890/dart_sdk.js:36191:9
Error: MissingPluginException(No implementation found for method loadBannerAd on channel
plugins.flutter.io/google_mobile_ads)
    at Object.throw_ [as throw] (http://localhost:54890/dart_sdk.js:5067:11)
    at platform_channel.MethodChannel.new._invokeMethod
    (http://localhost:54890/packages/flutter/src/services/restoration.dart.lib.js:1560:21)
    at _invokeMethod.next (<anonymous>)
    at http://localhost:54890/dart_sdk.js:40571:33
    at _RootZone.runUnary (http://localhost:54890/dart_sdk.js:40441:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:54890/dart_sdk.js:35363:29)
    at handleValueCallback (http://localhost:54890/dart_sdk.js:35931:49)
    at Function._propagateToListeners (http://localhost:54890/dart_sdk.js:35969:17)
    at _Future.new.[_completeWithValue] (http://localhost:54890/dart_sdk.js:35817:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:54890/dart_sdk.js:35838:35)
    at Object._microtaskLoop (http://localhost:54890/dart_sdk.js:40708:13)
    at _startMicrotaskLoop (http://localhost:54890/dart_sdk.js:40714:13)
    at http://localhost:54890/dart_sdk.js:36191:9
xmd2e60i

xmd2e60i1#

我遇到了同样的问题。请确保您在移动终端或模拟器上运行。我不认为这些方法在Chrome、Edge或Windows上运行时有效。
(我还做了其他事情来确保项目被清理,重新启动计算机,再次克隆git-repo等,但我相信这是解决丢失的方法)

相关问题