解决Flutter报错The method ‘File.create‘ has fewer named arguments than those of overridden method

问题描述

将Flutter SDK更新到3.7.9后,运行项目出来以下错误提示。

Could not build the precompiled application for the device.
Error (Xcode): ../../../.pub-cache/hosted/pub.flutter-io.cn/file-6.1.2/lib/src/interface/file.dart:15:16: Error: The method 'File.create' has fewer named arguments than those of overridden method 'File.create'.


Error launching application on iPhone.

解决方案

根据错误提示,是某个库依赖了google的file库的6.1.2版本,File.create方法需要几个参数。
我们进入pub中查看file的更新信息。
file|dart package:https://pub.flutter-io.cn/packages/file/changelog
file库版本更新日志
可以看到,从6.1.3开始,create方法增加了一个bool类型的参数,所以我们只需要将file库的版本升级到6.1.3以上即可。

项目中覆盖库版本

最粗暴的方式是直接在项目的pubspec.yaml中指定file库的版本,这样编译时会统一库的版本。

name: wiki assit
description: A new Flutter application 

# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.2.0+4

environment:
  sdk: ">=2.17.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  # 指定 file 版本,6.1.3及以上就行
  file: ^6.1.4

使用pub命令

因为不是在项目中直接引用的,所以只能通过pub命令的方式解决依赖。
首先,要清除一下缓存.

flutter clean.

然后使用pub upgrade更新依赖

flutter pub upgrade

等待依赖更新完成,重新运行项目就能正常跑起来了。

### 解决 Flutter 和 Java 交互中的 `MalformedInputException` 异常 当在 Flutter 中调用 Android 原生代码并处理字符串数据时,可能会遇到字符编码不匹配的问题,这可能导致 `java.nio.charset.MalformedInputException` 异常。此异常通常发生在尝试读取或写入带有非法字节序列的数据流时。 为了防止此类异常的发生,在传递字符串之前应确保两端使用的字符集一致,并且正确设置编码方式[^1]。 #### 设置统一的字符编码 建议显式指定 UTF-8 编码来避免潜在的乱码问题: ```java // 在Java端接收来自Flutter的消息时指明编码格式 String receivedMessage = new String(message.getBytes(), StandardCharsets.UTF_8); ``` 同样地,在发送消息回给 Dart 层面也需要保持相同的编码标准: ```java byte[] bytesToSendBack = response.getBytes(StandardCharsets.UTF_8); result.success(bytesToSendBack); ``` 通过这种方式可以有效减少由于不同平台默认采用不同的字符编码而导致的兼容性问题。 另外值得注意的是,如果确实存在非常短(如长度为1)的有效输入却仍然抛出了该异常,则可能是遇到了特殊字符或者是BOM(Byte Order Mark),此时可以在解析前先去除这些可能引起冲突的内容再做进一步处理[^3]。 对于特别情况下的单个字符引发异常的情况,可以通过调试工具查看具体是什么样的字符造成了这个问题,并针对性地调整处理逻辑以适应特殊情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值