Fluttertoast问题:成员未找到:“Overlay.maybeOf”

huangapple go评论74阅读模式
英文:

Fluttertoast problem: Member not found: 'Overlay.maybeOf'

问题

/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/fluttertoast-8.1.3/lib/fluttertoast.dart:165:28: Error: Member not found: 'Overlay.maybeOf'.
var _overlay = Overlay.maybeOf(context!);
^^^^^^^

/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/fluttertoast-8.1.3/lib/fluttertoast.dart:154:18: Error: The getter 'mounted' isn't defined for the class 'BuildContext'.

  • 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/framework.dart').
    framework.dart:1
    Try correcting the name to the name of an existing getter, or defining a getter or field named 'mounted'.
    if (context?.mounted != true) {
    ^^^^^^^

3

FAILURE: Build failed with an exception.

  • Where:
    Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1159

在将fluttertoast添加到我的项目后,当我在设备上运行项目时出现了这个问题。
这是什么意思?

我试图在出现错误或成功时显示toast。但是,除了snackbar之外,fluttertoast是唯一的选择。但在添加依赖项后,出现了这个问题。

英文:
    /C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/fluttertoast-8.1.3/lib/fluttertoast.dart:165:28: Error: Member not found: 'Overlay.maybeOf'.
    var _overlay = Overlay.maybeOf(context!);
                           ^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/fluttertoast-8.1.3/lib/fluttertoast.dart:154:18: Error: The getter 'mounted' isn't defined for the class 'BuildContext'.
- 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/framework.dart').
framework.dart:1
Try correcting the name to the name of an existing getter, or defining a getter or field named 'mounted'.
    if (context?.mounted != true) {
                 ^^^^^^^
3

FAILURE: Build failed with an exception.

* Where:
Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1159

After adding fluttertoast in my project it shows this problem while I run project on my device.
Now, what does it mean?

I was trying to show toast when an error or success occurred. But without snackbar fluttertoast is the only option to do.
But after adding dependency this problem showed.

答案1

得分: 11

更新:找到了实际问题:

Flutter toast v8.1.3 无法在低于 3.7.0 版本的 Flutter 上工作,因为在 Flutter v3.7.0 中有一个 重大变更。您必须要么使用 fluttertoast v8.1.2,要么将您的 Flutter 版本升级到 v3.7.0 或更高版本。

旧回答:

在运行 flutter pub upgrade 后,我遇到了类似的问题,但即使在 pubspec.yaml 文件中回滚更改后,我仍然收到相同的错误。但在回滚 pubspec.lock 文件中的更改后,这个问题得到了解决。确保在恢复 pubspec.lock 文件中的更改后运行 flutter clean

英文:

Update: Found the actual issue:

Flutter toast v8.1.3 cannot work with a Flutter version older than 3.7.0 because of a breaking change in Flutter v3.7.0. You have to either use fluttertoast v8.1.2 or you have to upgrade your flutter version to v3.7.0+.

Old Answer

I faced a similar issue after running the flutter pub upgrade but even after rolling back the changes in the pubspec.yaml file, I got the same error. But this issue was fixed after rolling back the changes in the pubspec.lock file. Make sure to do flutter clean after reverting changes in pubspec.lock file.

答案2

得分: 5

由于某些限制,我无法更改Flutter版本,并且更改fluttertoast的版本也没有取得任何成功。所以我尝试了一些其他替代方案。

我从项目中完全移除了fluttertoast,并通过在终端中运行以下命令安装了一个新的依赖项:

flutter pub add toast

用法:

Toast.show("Toast plugin app", duration: Toast.lengthShort, gravity: Toast.bottom);
英文:

I also came across the same problem,
Due to some limitations I was not able to change the flutter version, and Changing the version of fluttertoast has not led to any success. so what I do is try some other alternatives

toast: ^0.3.0
https://pub.dev/packages/toast

I removed fluttertoast completely from the project and install a new dependency by running the command in the terminal

flutter pub add toast

usage:

Toast.show("Toast plugin app", duration: Toast.lengthShort, gravity:  Toast.bottom);

答案3

得分: 0

尝试使用这个:

 if (!mounted) return;
 snackBar(yourText, context);//或者使用你的fluttertoast

如果您遇到任何问题或有问题,我在这里回答。

愉快的编码。

英文:

try to use this:

 if (!mounted) return;
 snackBar(yourText, context);//or use your fluttertoast

if you had any problem or question i'm here to answer.

happy coding.

答案4

得分: 0

抱歉,此错误是由于fluttertoast包与您的Flutter版本不兼容引起的。Flutter 2.0 中已移除 Overlay.maybeOf 方法,并替换为 Overlay.of,这可能是fluttertoast包正在使用的方法。

要解决此错误,您可以尝试升级到最新版本的fluttertoast,或者如果那不起作用,降级您的Flutter版本以与您正在使用的fluttertoast版本兼容。

英文:

Unfortunately, this error is caused by a version incompatibility between the fluttertoast package and your version of Flutter. The Overlay.maybeOf method was removed in Flutter 2.0 and replaced with Overlay.of, which is likely what the fluttertoast package is using.

In order to fix this error, you can try upgrading to the latest version of fluttertoast, or if that doesn't work, downgrade your version of Flutter to a version compatible with the version of fluttertoast you are using.

huangapple
  • 本文由 发表于 2023年2月10日 12:25:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定