【发布时间】:2020-08-14 09:34:54
【问题描述】:
我正在尝试从 Firebase 下载文件并将其保存在我的设备上。 我正在关注这个例子:
我收到一个错误,即 childName 不能为 null 或为空,但文件存在且不为空。 当我打印文件的内容时,它显示它是空的。 我做错了什么?
这是我的代码:
import 'dart:io';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class ImportExcel extends StatefulWidget {
@override
_ImportExcelState createState() => _ImportExcelState();
}
class _ImportExcelState extends State<ImportExcel> {
final String kTestString = 'testFile';
Future<void> _downloadFile(StorageReference ref) async {
final String filePath = 'example.txt';
final String url = await ref.child(filePath).getDownloadURL();
final http.Response downloadData = await http.get(url);
final Directory systemTempDir = Directory.systemTemp;
final File tempFile = File('${systemTempDir.path}/temp$filePath');
if (tempFile.existsSync()) {
await tempFile.delete();
}
await tempFile.create();
assert(await tempFile.readAsString() == "");
final StorageFileDownloadTask task = ref.writeToFile(tempFile);
print(task);
final String tempFileContents = await tempFile.readAsString();
print('Filecontent $tempFileContents');
print('TestString $kTestString');
assert(tempFileContents == kTestString);
final int byteCount = (await task.future).totalByteCount;
print(byteCount);
final String fileContents = downloadData.body;
print(fileContents);
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton.extended(
onPressed: () async {
StorageReference ref = FirebaseStorage.instance.ref();
_downloadFile(ref);
},
label: Text('Import Data')),
);
}
}
这是我得到的错误:
I/flutter (16195): Instance of 'StorageFileDownloadTask'
E/MethodChannel#plugins.flutter.io/firebase_storage(16195): Failed to handle method call
E/MethodChannel#plugins.flutter.io/firebase_storage(16195): java.lang.IllegalArgumentException: childName cannot be null or empty
E/MethodChannel#plugins.flutter.io/firebase_storage(16195): at com.google.android.gms.common.internal.Preconditions.checkArgument(Unknown Source:35)
E/MethodChannel#plugins.flutter.io/firebase_storage(16195): at com.google.firebase.storage.StorageReference.child(com.google.firebase:firebase-storage@@17.0.0:84)
E/MethodChannel#plugins.flutter.io/firebase_storage(16195): at io.flutter.plugins.firebase.storage.FirebaseStoragePlugin.writeToFile(FirebaseStoragePlugin.java:383)
【问题讨论】:
标签: firebase flutter dart firebase-storage