Flutter中“No implementation found for method showToast”如何解决?

问题描述 投票:0回答:8

我有 https://pub.dev/packages/fluttertoast,flutter 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)

我的意思很奇怪。我就是不明白到底发生了什么?有人能解释一下如何克服这个问题吗?

<---------------EDITS:------------------->

有关代码的详细信息:

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")
              ],
            ),
          ),
        );
      }
    }
flutter dart toast
8个回答
21
投票

运行 flutter clean 并从您的设备/模拟器卸载以前安装的应用程序并重新构建。它应该工作。


6
投票
  • 扑干净
  • flutter pub get

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


2
投票

这里的所有答案都很好,所以在前面的答案中添加另一个选项。

也许你正试图在不受支持的平台上运行它

我在 Linux 中运行时遇到与您完全相同的错误。


1
投票

对于那些仍然遇到这个问题的人,只需在

return
之前添加一个
Fluttertoast.showToast()
声明就可以解决我的问题。

                return Fluttertoast.showToast(
                  msg: 'Toast Message',
                  toastLength: Toast.LENGTH_SHORT,
                  gravity: ToastGravity.SNACKBAR,
                  backgroundColor: Colors.blueGrey,
                  textColor: Colors.white,
                  fontSize: 16.0,
                );

1
投票

简单地重新启动项目对我有用。


0
投票

似乎这个问题目前无法解决,但你可以使用this

添加此依赖项:

toast: ^0.1.5

然后像这样简单地使用:

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

0
投票

将库降级为:

fluttertoast:^8.0.9


0
投票

我有同样的问题,
我使用的是最新版本的 flutter Toast: 颤振吐司:^8.2.1

  1. 我用这个版本
    fluttertoast: ^8.0.9
  2. flutter clean
  3. flutter pub get
  4. run
© www.soinside.com 2019 - 2024. All rights reserved.