Flutter 将文件上传到 Firebase 存储

问题描述 投票:0回答:1

有人可以帮助我吗?我想将文件上传到 Firebase 存储,但由于某些原因此代码不起作用。谁能帮帮我吗?

class TestUploadToStorage extends StatefulWidget {
  const TestUploadToStorage({super.key});

  @override
  State<TestUploadToStorage> createState() => _TestUploadToStorageState();
}

class _TestUploadToStorageState extends State<TestUploadToStorage> {
  PlatformFile? testFile;

  Future selectTestFile() async {
    final result = await FilePicker.platform.pickFiles();
    if (result == null) result;
    setState(() {
      testFile = result?.files.first;
    });
  }

  Future uploadTestFile() async {
    final pathTest = "TESTFILE/${testFile!.name}";
    final fileTest = File(testFile!.path!);
    final testRef = FirebaseStorage.instance.ref().child(pathTest);
    testRef.putFile(fileTest);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: [
            if (testFile != null) Text(testFile!.name),
            TextButton(
              onPressed: selectTestFile,
              child: const Text("select TestFile"),
            ),
            TextButton(
                onPressed: uploadTestFile, child: const Text("Test upload"))
          ],
        ),
      ),
    );
  }
}

我使用的是 Mac M1,我的应用程序是网络应用程序

flutter google-cloud-storage filepicker
1个回答
0
投票

我遇到了类似的问题,这就是你应该做的。

将await放在

putFile
方法之前。

await testRef.putFile(fileTest);

虽然 putFile 方法不返回 Future Value,等待它很奇怪,但它对我有用。

希望对你有帮助

© www.soinside.com 2019 - 2024. All rights reserved.