颤振/飞镖错误:参数类型为'Future< File>'无法分配给参数类型“文件” [英] flutter / dart error: The argument type 'Future<File>' can't be assigned to the parameter type 'File'

查看:64
本文介绍了颤振/飞镖错误:参数类型为'Future< File>'无法分配给参数类型“文件”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to build my first mobile application with flutter and firebase. When I try to display and store a photo I have the following issue :


错误:参数类型'Future'可以不会分配给参数类型文件。 (argument_type_not_assignable位于[whereassistant] lib / main.dart:85)

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

我可能应该进行一些强制转换,但我不了解hox正确执行操作。

I should probably do some casting but I don't understand hox to do it properly.

这是我的Future文件声明:

Here's my Future file declaration :

Future<File> _imageFile;

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

I'm taking a Photo which is displayed on screen :

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

但是尝试将照片发送到Firebase时出现错误:

But I have the error when trying to send the photo to Firebase :

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

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

Here's the class I'm using based on a code example :

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>

ref.put asks for a File as parameter. What you are passing is a Future<File>.

您需要等待未来的结果才能拨打电话。

You need to wait for the result of that future to make your call.

您可以更改代码

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

或将 _imageFile 更改为文件而不是 Future<文件>

这篇关于颤振/飞镖错误:参数类型为'Future&lt; File&gt;'无法分配给参数类型“文件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆