无效参数:实例为 ‘DateTime’

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

Invalid argument: Instance of 'DateTime'

问题

I am working on a Flutter plugin, and I would like to pass from my dart file Native Android a DateTime parameter.

The implementation is simple:

Dart

  Future<bool?> getDate(DateTime localTime) async {
      return _dateChannel.invokeMethod<bool>('my_method_channel',
          {'localTime': localTime});
  }

Java

if (methodCall.method.equals("my_method_channel")) {
    Date localTime = methodCall.argument("localTime");

    System.out.println("TEST | Estimated localTime | " + localTime);

    result.success(true);
}

but I am getting the following error

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument: Instance of 'DateTime'

Note that this is a simplification of my implementation. I understand that I could get localTime natively, but that's not what I am looking for.

Explanation of the current situation and how to solve it.

英文:

I am working on a Flutter plugin, and I would like to pass from my dart file Native Android a DateTime parameter.

The implementation is simple:

Dart

  Future&lt;bool?&gt; getDate(DateTime localTime) async {
      return         _dateChannel.invokeMethod&lt;bool&gt;(&#39;my_method_channel&#39;,
          {&#39;localTime&#39;: localTime});
  }

Java

if (methodCall.method.equals(&quot;my_method_channel&quot;)) {
            Date localTime = methodCall.argument(&quot;localTime&quot;);

            System.out.println(
                    &quot;TEST |  Estimated localTime | &quot; + localTime);

            result.success(true);
}

but I am getting the following error

> [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument: Instance of 'DateTime'

Note that this is a simplication of my implementation. I understand that I could get localTIme natively but that's not what I am looking for.

Explanation of the current situation and how to solve it.

答案1

得分: 3

日期/日期时间数据类型在平台通道中不受支持。您应该将参数传递为字符串类型,然后在Java代码中进行解析。

链接:https://docs.flutter.dev/platform-integration/platform-channels?tab=type-mappings-java-tab#codec

英文:

Data type Date/Datetime is not supported for platform channel. You should pass an argument as String type and then parse it in Java code.

https://docs.flutter.dev/platform-integration/platform-channels?tab=type-mappings-java-tab#codec

huangapple
  • 本文由 发表于 2023年6月29日 21:11:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581408.html
匿名

发表评论

匿名网友

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

确定