如何使用Flutter网站中的Universal_html插件将字符串转换为Blob

问题描述

我正在尝试使用Flutter网络应用程序显示Google云端硬盘文件。在此阶段,我能做的最好的就是在浏览器中打开pdf网址。我可以在Flutter网站中使用Google Apps脚本从Google云端硬盘文件获取blob字符串,但是当我使用 universal_html 插件打开pdf时,无法将字符串转换为blob对象和Flutter项目显示blob时,会在控制台中引发以下错误

问题: 如何将Blob字符串从Google Apps脚本转换为可以在html.Url.createObjectURLFromBlob(blob);中使用的Blob对象; ?

第1步:设置Google Apps脚本项目。

function doGet(){
  var blobText = getFileAsBlob("1wzR-7fWT_vA7jVSUnDwSj28j82scazkkup8Ovr9kwjv");
  return ContentService.createtextoutput(blobText).setMimeType(ContentService.MimeType.JSON);
  //return HtmlService.createHtmlOutput(blobText);
}

//Return file as blob by Id
function getFileAsBlob(fileId) {
  var file = DriveApp.getFileById(fileId);
  var blob = file.getBlob().getDataAsstring();
  return blob;
}
@H_404_15@

第二步:设置Flutter项目。在pubspec.yaml中添加依赖项

import 'package:Flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:universal_html/html.dart' as html;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'PDF viewer demo',theme: ThemeData(
        primarySwatch: Colors.blue,visualDensity: VisualDensity.adaptivePlatformDensity,),home: MyHomePage(title: 'Show Google Drive file in Flutter'),);
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key,this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String path;

  Future<String> _getFileFromGoogleDrive() async {
    final driveUrl =
        "https://script.google.com/macros/s/AKfycbzY-XQxMfq6KeeRL_-4y8DnnBanu3PX813Ipfz8xogD7dgQKb2Y/exec";

    return await http.get(driveUrl).then((response) {
      print(response.body); //Error
      //Todo:
      html.Blob blob = new html.BlobText(response.body);
      final url = html.Url.createObjectURLFromBlob(blob);
      html.window.open(url,"_blank");
      html.Url.revokeObjectUrl(url);
      return null;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
            Text("Click to open file."),],floatingActionButton: FloatingActionButton(
        onpressed: _getFileFromGoogleDrive,tooltip: 'Show file',child: Icon(Icons.file_present),);
  }
}
@H_404_15@

错误:按“显示文件”按钮时出现颤振错误

Bad UTF-8 encoding found while decoding string: %PDF-1.5
%����
2 0 obj
<< /Linearized 1 /L 17762 /H [ 750 126 ] /O 6 /E 17487 /N 1 /T 17486 >>
endobj
                                                                                                                 
3 0 obj
<< /Type /XRef /Length 51 /Filter /FlateDecode /DecodeParms << /Columns 4 /Predictor 12 >> /W [ 1 2 1 ] /Index [ 2 24 ] /Info 11 0 R /Root 4 0 R /Size 26 /Prev 17487                 /ID [<439e80749f258bfcf6a670df0083ad46><439e80749f258bfcf6a670df0083ad46>] >>
stream
x�cbd�g`b`8 $���XF@��Dh   ! af�Me`b<p����b
@H_404_15@


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)