firebase - flutter/Dart 错误 : The argument type 'Future<File>' can't be assigned to the parameter type 'File'

标签 firebase dart future flutter

我正在尝试使用 Flutter 和 Firebase 构建我的第一个移动应用程序。 当我尝试显示和存储照片时,出现以下问题:

error: The argument type 'Future' can't be assigned to the parameter type 'File'. (argument_type_not_assignable at [whereassistant] lib/main.dart:85)

我可能应该做一些 Actor ,但我不明白如何正确地做。

这是我的 future 文件声明:

Future<File> _imageFile;

我正在拍摄一张显示在屏幕上的照片:

    setState(() {
      _imageFile = ImagePicker.pickImage(source: source);
    });

但我在尝试将照片发送到 Firebase 时遇到错误:

    final StorageUploadTask uploadTask = ref.put(_imageFile);
    final Uri downloadUrl = (await uploadTask.future).downloadUrl;

这是我使用的基于代码示例的类:

class _MyHomePageState extends State<MyHomePage> {
  Future<File> _imageFile;

  void _onImageButtonPressed(ImageSource source) async {
    GoogleSignIn _googleSignIn = new GoogleSignIn();
    var account = await _googleSignIn.signIn();
    final GoogleSignInAuthentication googleAuth = await account.authentication;
    final FirebaseUser user = await _auth.signInWithGoogle(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );
    assert(user.email != null);
    assert(user.displayName != null);
    assert(!user.isAnonymous);
    assert(await user.getIdToken() != null);

    final FirebaseUser currentUser = await _auth.currentUser();
    assert(user.uid == currentUser.uid);

    setState(() {
      _imageFile = ImagePicker.pickImage(source: source);
    });
    var random = new Random().nextInt(10000);
    var ref = FirebaseStorage.instance.ref().child('image_$random.jpg');
    final StorageUploadTask uploadTask = ref.put(_imageFile);
    final Uri downloadUrl = (await uploadTask.future).downloadUrl;
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: const Text('Where Assistant'),
      ),
      body: new Center(
        child: new FutureBuilder<File>(
          future: _imageFile,
          builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
            debugPrint('test recup image');
            print(snapshot);

            if (snapshot.connectionState == ConnectionState.done &&
                snapshot.data != null) {
              return new Image.file(snapshot.data);
            } else if (snapshot.error != null) {
              return const Text('Error picking image.');
            } else {
              return const Text('No image so far.');
            }
          },
        ),
      ),
      floatingActionButton: new Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          new FloatingActionButton(
            onPressed: () => _onImageButtonPressed(ImageSource.gallery),
            tooltip: 'Pick an image from gallery',
            child: new Icon(Icons.photo_library),
          ),
          new Padding(
            padding: const EdgeInsets.only(top: 16.0),
            child: new FloatingActionButton(
              onPressed: () => _onImageButtonPressed(ImageSource.camera),
              tooltip: 'Take a Photo',
              child: new Icon(Icons.camera_alt),
            ),
          ),
        ],
      ),
    );
  }
}

最佳答案

ref.put要求 File作为参数。您传递的是 Future<File> .

您需要等待 future 的结果才能调用电话。

您可以将代码更改为

final StorageUploadTask uploadTask = ref.put(await _imageFile);
final Uri downloadUrl = (await uploadTask.future).downloadUrl;

或更改_imageFileFile而不是 Future<File>

关于firebase - flutter/Dart 错误 : The argument type 'Future<File>' can't be assigned to the parameter type 'File' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49854024/

相关文章:

Dart 扩展 : don't access members with 'this' unless avoiding shadowing

java - ListenableFuture 链可以处理内部 ExecutionException 吗?

java - ThreadPoolExecutor, future : correlating requests and responses

scala - 运行 future n 次

api - 如何在 flutter 中向 API 服务器发送请求 POST 消息?

flutter - 在Flutter中读取 'lib/generated_plugin_registrant.dart'时出错

firebase - ffmpeg 参数与 flutter 视频上传到 firebase

ios - 推送通知不起作用(仅在 Debug模式下有效)

JavaScript 数组不可迭代

json - 在 JSON 树数据库中存储聊天的最佳方式?