为什么ByteData.view(buffer)抛出NoSuchMethodError:Class' String'没有实例方法' asByteData'

时间:2018-06-17 16:56:26

标签: casting dart udp buffer

我正在尝试读取UDP数据并将其转换为浮点数。

我知道我需要

  1. 从RawDatagramSocket接收数据报
  2. 从数据报中获取ByteBuffer
  3. 将ByteBuffer转换为ByteData
  4. 在ByteData上使用getFloat32方法
  5. 这是破碎的代码:

    import 'dart:io';
    import 'dart:typed_data';
    import 'dart:mirrors';
    
    getTypeName(dynamic obj) {
      return reflect(obj).type.reflectedType.toString();
    }
    
    void main(List<String> args){
      RawDatagramSocket.bind("192.168.1.1", 4444).then((RawDatagramSocket socket){
        print('Datagram socket ready to receive');
        print('${socket.address.address}:${socket.port}');
        socket.listen((RawSocketEvent e){
          Datagram d = socket.receive();
          if (d == null) return;
          ByteBuffer buffer = getTypeName(d.data.buffer);
          ByteData bdata = new ByteData.view(buffer);
        });
      });
    }
    

    以下是调试代码:

    import 'dart:io';
    import 'dart:typed_data';
    import 'dart:mirrors';
    
    getTypeName(dynamic obj) {
      return reflect(obj).type.reflectedType.toString();
    }
    
    void main(List<String> args){
      RawDatagramSocket.bind("192.168.1.1", 4444).then((RawDatagramSocket socket){
        print('Datagram socket ready to receive');
        print('${socket.address.address}:${socket.port}');
        socket.listen((RawSocketEvent e){
          Datagram d = socket.receive();
          if (d == null) return;
          print(d.data);
        });
      });
    }
    

    如果我使用以下命令nc -u 192.168.1.1 4444启动netcat并发送数据&#39; 1111&#39;,我会从调试代码中打印出以下内容:

    [49, 49, 49, 49, 10]
    

    我理解这些价值观是&#39; 1111&#39;然后是回车。

    问题是,当我运行“破解的代码”时,我最终收到以下消息:

    Unhandled exception:
    NoSuchMethodError: Class 'String' has no instance method 'asByteData'.
    Receiver: "_ByteBuffer"
    Tried calling: asByteData(0, null)
    #0      Object._noSuchMethod (dart:core-patch/object_patch.dart:43)
    #1      Object.noSuchMethod (dart:core-patch/object_patch.dart:47)
    #2      new ByteData.view (dart:typed_data:449)
    #3      main.<anonymous closure>.<anonymous closure> (file:///Users/iProgram/test.dart:17:25)
    #4      _RootZone.runUnaryGuarded (dart:async/zone.dart:1307)
    #5      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:330)
    #6      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:257)
    #7      _StreamController&&_SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:796)
    #8      _StreamController._add (dart:async/stream_controller.dart:667)
    #9      _StreamController.add (dart:async/stream_controller.dart:613)
    #10     new _RawDatagramSocket.<anonymous closure> (dart:io-patch/socket_patch.dart:1699)
    #11     _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart:760)
    #12     _microtaskLoop (dart:async/schedule_microtask.dart:41)
    #13     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50)
    #14     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:99)
    #15     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:152)
    

    问题是,当我没有使用字符串或asByteData命令时,为什么会出现错误Class 'String' has no instance method 'asByteData'

    我一直在使用以下文档:Dart - ByteData

    由于

1 个答案:

答案 0 :(得分:2)

  

为什么我得到错误类'String'在我没有使用字符串或asByteData命令时没有实例方法'asByteData'?

调用堆栈显示了这里发生的事情:

Tried calling: asByteData(0, null)
#0      Object._noSuchMethod (dart:core-patch/object_patch.dart:43)
#1      Object.noSuchMethod (dart:core-patch/object_patch.dart:47)
#2      new ByteData.view (dart:typed_data:449)

您正在呼叫内部呼叫new ByteData.view的{​​{1}}。

asByteData构造函数需要ByteData.view,但您传递的是字符串:

ByteBuffer

虽然您已在此处撰写ByteBuffer buffer = getTypeName(d.data.buffer); ,但您的ByteBuffer功能实际上正在返回getTypeName()

String