【问题标题】:Problem downloading a file from Firebase in flutter从 Firebase 下载文件时出现问题
【发布时间】:2020-08-14 09:34:54
【问题描述】:

我正在尝试从 Firebase 下载文件并将其保存在我的设备上。 我正在关注这个例子:

https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_storage/example/lib/main.dart

我收到一个错误,即 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


    【解决方案1】:

    这似乎是错误的:

    final StorageFileDownloadTask task = ref.writeToFile(tempFile);
    

    您的ref 变量是StorageReference ref = FirebaseStorage.instance.ref(),这意味着您正在尝试下载整个存储桶。

    你更可能想要:

    var fileRef = ref.child(filePath);
    final String url = await fileRef.getDownloadURL();
    ...
    final StorageFileDownloadTask task = fileRef.writeToFile(tempFile);
    

    【讨论】:

      猜你喜欢
      • 2020-01-04
      • 2013-08-11
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      相关资源
      最近更新 更多