如何解决Flutter中出现的"No implementation found for method showToast"错误?

12

我最初使用了https://pub.dev/packages/fluttertoast,Flutter Toast很好地运行。我也得到了完美的Toast输出。

在我实现admob代码后,Flutter Toast不再工作了。它一直抛出以下错误:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method showToast on channel PonnamKarthik/fluttertoast)

这让我感到很奇怪。我根本不明白到底发生了什么事情?有人能解释一下如何克服这个问题吗?

<---------------编辑:------------------->

关于代码的细节:

import.............

    Future<void> callback() async {
      print("I am in the isolate");
      // DateTime now=DateTime.now().toLocal();
    //  print("Date time:");
      // print(now);

      Fluttertoast.showToast(
          msg:
          "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
          toastLength: Toast.LENGTH_LONG,
          gravity: ToastGravity.BOTTOM,
          backgroundColor: Colors.black,
          textColor: Colors.red,
          fontSize: 16.0);
      print("last line of Isolate");
    }
void alarm_managing_function(int t) async {
  await AndroidAlarmManager.initialize();
  await AndroidAlarmManager.periodic(Duration(seconds: t), 0, callback);
}

    .
    .

    .
    .

    .

    ..
    .
    .
    class AlertTime2 extends StatefulWidget {
      static const routeName = '/alertTime2';

      @override
      _AlertTime2State createState() => _AlertTime2State();
    }

    class _AlertTime2State extends State<AlertTime2> {


      String mm = "00";
      String ss = "00";
      var mmValue;
      var ssValue;
      int totalTimeInSec;
      int actualTimeInSec;
      String _selectedTime;
      static final MobileAdTargetingInfo targetInfo = new MobileAdTargetingInfo(
        testDevices: <String>[],

      );

      BannerAd _bannerAd;
      InterstitialAd _interstitialAd;

      BannerAd createBannerAd() {
        return new BannerAd(
            adUnitId: "xxxxxxxxxxxxxxxxxxxxxxxx", size: AdSize.banner,
            targetingInfo: targetInfo, listener: (MobileAdEvent event) {
          print("Banner event : $event");
        });
      }


      InterstitialAd createInterstitialAd() {
        return new InterstitialAd(
            adUnitId: "xxxxxxxxxxxxxxxxxxxxxx",
            targetingInfo: targetInfo, listener: (MobileAdEvent event) {
          print("Interstitial event : $event");
        });
      }

      @override
      void initState() {
        super.initState();
        FirebaseAdMob.instance.initialize(
            appId: "xxxxxxxxxxxxxxxxxxxxxx");
        _bannerAd = createBannerAd()
          ..load()
          ..show();
        initPlatformState();

        FileUtils().readFromFile().then((String value) {
          setState(() {
            _selectedTime = value;
          });
        });
      }

      @override
      void dispose() {
        super.dispose();
        _bannerAd?.dispose();
        _interstitialAd?.dispose();
      }


         @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.fromLTRB(30, 70, 0, 0),
                  child: Text(
                    "Timer for Notification",
                    style: TextStyle(
                      fontSize: 50,
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(0, 150, 0, 0),
                  child: Text(
                    "MM-SS",
                    style: TextStyle(
                      fontSize: 30,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
                  child: Text(
                    "${_selectedTime ?? 'Selected Time'}",
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 38),
                  ),
                ),
                RaisedButton(
                  onPressed: () {
                    DatePicker.showPicker(context,
                        showTitleActions: true,
                        pickerModel:
                        CustomPicker(currentTime: DateTime.tryParse(_selectedTime)),
                        onConfirm: (time) {
                          setState(() {
                            print(time);
                            _selectedTime = DateFormat("mm-ss").format(time);
                            FileUtils().saveToFile(_selectedTime);

                            mm = _selectedTime.substring(0, 2);
                            ss = _selectedTime.substring(3, 5);
                            print(mm);
                            print(ss);
                            mmValue = int.parse(mm);
                            ssValue = int.parse(ss);
                            totalTimeInSec = mmValue * 60 + ssValue;
                            print(totalTimeInSec);

                            DateTime now = DateTime.now().toLocal();
                            print("Start-->Date time:");
                            print(now);

                            time_storer(totalTimeInSec);
                            alarm_managing_function(totalTimeInSec);
                          });
                        }, locale: LocaleType.en);
                  },
                  child: Text("Show Time picker"),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(80, 10, 30, 0),

                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(0, 70, 0, 0),
                  child: IconButton(


                    icon: Icon(Icons.arrow_back),

                    iconSize: 70,
                    color: Colors.blue,
                    onPressed: () {
                      _interstitialAd = createInterstitialAd()
                        ..load()
                        ..show();
                      Navigator.pop(context);
                    },

                  ),
                ),
                Text("Back to Home")
              ],
            ),
          ),
        );
      }
    }

你能否发布引入AdMob代码的代码片段以及最少量的周围代码?请删除任何私人信息。这将有助于提供上下文。 - ryanwebjackson
2
@ryanwebjackson,非常感谢您的回复亲爱的......我已经在编辑中包含了代码。 - Chethan CV
10个回答

28

运行flutter clean命令并卸载先前在您的设备/模拟器上安装的应用程序,然后重新构建即可。这样应该可以解决问题。


7
  • 运行 flutter clean 命令
  • 运行 flutter pub get 命令

在终端中运行以上两个命令,然后重新启动应用程序,它应该正常工作。


5
所有的回答都很好,所以在之前的答案中再添加一个选项。也许你正在尝试在不支持的平台上运行它。我在Linux上运行时遇到了和你完全相同的错误。请参考以下链接:enter image description here

这是我的问题,因为我正在使用Windows模拟器,而我的设置只支持Android/IOS/Web。我选择继续使用Web平台,它能正常运行。 - borj

1
对于仍然遇到这个问题的人,只需在Fluttertoast.showToast()之前加上return语句即可解决该问题。
                return Fluttertoast.showToast(
                  msg: 'Toast Message',
                  toastLength: Toast.LENGTH_SHORT,
                  gravity: ToastGravity.SNACKBAR,
                  backgroundColor: Colors.blueGrey,
                  textColor: Colors.white,
                  fontSize: 16.0,
                );

1

看起来这个问题目前无法解决,但你可以使用this

添加这个依赖项:toast: ^0.1.5

然后简单地像这样使用:

Toast.show("Toast plugin app", context, duration: Toast.LENGTH_LONG, gravity:  Toast.BOTTOM);

简单而实用的! - 蒋艾伦

1

仅仅重新启动项目对我有用。


0
对我来说,我尝试了上面的所有解决方案,但当我重新启动Android Studio时都没有起作用。顺便说一下,我删除了模拟器后,一切都正常工作了!

这并没有真正回答这个问题。如果你有其他问题,可以点击提问来提出。如果你想在这个问题有新的回答时收到通知,你可以关注该问题。一旦你拥有足够的声望,你还可以添加悬赏以吸引更多关注。- 来自评论 - BoP

0

我也有同样的问题,
我使用了最新版本的Flutter Toast: fluttertoast:^8.2.1

  1. 我使用的是这个版本 fluttertoast:^8.0.9
  2. flutter clean
  3. flutter pub get
  4. run

0

将库降级至:

fluttertoast: ^8.0.9


0
fluttertoast: ^8.2.2
 void fLuttertoastnotification(String label) {
    Fluttertoast.showToast(
      msg: 'Welcome $label'
          .toUpperCase(),
      toastLength: Toast.LENGTH_LONG,
      timeInSecForIosWeb: 2,
      backgroundColor: Colors.black,
      textColor: Colors.white,
      fontSize: 25,
      gravity: ToastGravity.TOP,
    );
  }
flutter clean and then flutter run it will work

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接