flutter / dart error: The argument type 'Future' can't be assigned to the parameter type 'File'

前端 未结 4 1698
时光说笑
时光说笑 2021-02-14 06:12

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 :

error: T

相关标签:
4条回答
  • 2021-02-14 06:58

    For those who are still looking for answers, as it seems there are different reasons for the same error. In my case, it was the different import statement for a file which I was passing as parameters to a different function. It should be same file(import) in case of declaration and definition.

    for example, don't use like this in dart:

    import 'GitRepoResponse.dart';
    import 'GitRowItem.dart';
    

    and then in some another class

    import 'package:git_repo_flutter/GitRepoResponse.dart';
    import 'package:git_repo_flutter/GitRowItem.dart';
    

    because

    In Dart, two libraries are the same if, and only if, they are imported using the same URI. If two different URIs are used, even if they resolve to the same file, they will be considered to be two different libraries and the types within the file will occur twice

    Read more here

    0 讨论(0)
  • 2021-02-14 07:09

    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.

    You can change your code to

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

    Or change _imageFile to File instead of Future<File>

    0 讨论(0)
  • 2021-02-14 07:11

    to convert Future<File> to File, add await in front of the function to make its argument a Future type!

    File file2 = await fixExifRotation(imageFile.path);
    
    0 讨论(0)
  • 2021-02-14 07:13

    From the plugin README.md

      Future getImage() async {
        var image = await ImagePicker.pickImage(source: ImageSource.camera);
    
        setState(() {
          _image = image;
        });
      }
    

    ImagePicker.pickImage() returns a Future. You can use async/await like shown in the code above the get the value from the Future.

    0 讨论(0)
提交回复
热议问题