未捕获错误:类'int'没有实例getter'length'

时间:2014-12-24 23:43:09

标签: dart google-drive-api google-apis-explorer

我是否可能错误地创建了流媒体实例?

修改googleapis_examples/drive_upload_download_console,我正在尝试将已存储的.docx,.xlsx等文件转换为相应的Google云端硬盘版本。

以下代码

import 'dart:async';
import 'dart:io';

import 'package:http/http.dart' show Client;
import 'package:googleapis/common/common.dart' show Media, DownloadOptions;
import 'package:googleapis/drive/v2.dart' as drive;
import 'package:path/path.dart' as path;

Future convertFile(drive.DriveApi api, 
                    Client client, 
                    String objectId) {
  var completer = new Completer();
  api.files.get(objectId).then((drive.File file) {
    var fileName = path.basenameWithoutExtension(file.title);
    var parents = file.parents;

    client.readBytes(file.downloadUrl).then((bytes) {
      var driveFile = new drive.File()
      ..title = fileName
      ..mimeType = 'application/vnd.google-apps.document'
      ..parents = parents;
      api.files.insert(driveFile)
      .then((driveFile){
        var byteList = bytes.toList();
        var stream = new Stream.fromIterable(byteList);
        var media = new Media(stream, byteList.length);
        api.files.update(new drive.File(), driveFile.id, uploadMedia: media)
        .then((drive.File f){
          stream.close().whenComplete((){
            api.files.delete(objectId)
            .then((_){
              completer.complete(true);
              print("Converted ${f.id}");
            });
          });
        });
      });
    });
  });
  return completer.future;
}

会导致以下错误。

Unhandled exception:
Uncaught Error: Class 'int' has no instance getter 'length'.

NoSuchMethodError: method not found: 'length'
Receiver: 80
Arguments: []
Stack Trace:
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#1      Base64Encoder.bind.onData (package:googleapis/src/common_internal.dart:325:42)
#2      _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#3      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341)
#4      _IterablePendingEvents.handleNext (dart:async/stream_impl.dart:549)
#5      _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:671)
#6      _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#7      _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#8      _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:84)
#9      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:131)

#0      _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:886)
#1      _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#2      _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#3      _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:84)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:131)

1 个答案:

答案 0 :(得分:4)

Media构造函数需要Stream<List<int>>,您的代码提供Stream<int>。要解决此问题,只需替换此行:

 var stream = new Stream.fromIterable(byteList);

由:

 var stream = new Stream.fromIterable([byteList]);