1

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

修改googleapis_examples/drive_upload_download_console,我正在尝试将已存储的 .docx、.xlsx 等文件转换为相应的 Google Drive 对应文件。

以下代码

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)
4

1 回答 1

5

Media 构造函数需要一个Stream<List<int>>,而您的代码提供了一个Stream<int>. 要解决这个问题,只需替换这一行:

 var stream = new Stream.fromIterable(byteList);

经过:

 var stream = new Stream.fromIterable([byteList]);
于 2014-12-25T17:54:31.813 回答