Flutter 类型';FutureBuilder<;文件>';不是类型为';图像提供商<;动态>';-图像选择器问题

Flutter 类型';FutureBuilder<;文件>';不是类型为';图像提供商<;动态>';-图像选择器问题,flutter,dart,future,imagepicker,Flutter,Dart,Future,Imagepicker,我需要在容器中添加一个图像。图像来自图像选择器。 我得到了一个错误: type 'FutureBuilder<File>' is not a subtype of type 'ImageProvider<dynamic>' 这也不管用 我想我用尽了这里所有可能的排列 顺便说一下,这里是_getLocalFile Future<io.File> _getLocalFile(String filename) async { io.File f

我需要在容器中添加一个图像。图像来自图像选择器。 我得到了一个错误:

 type 'FutureBuilder<File>' is not a subtype of type 'ImageProvider<dynamic>'
这也不管用

我想我用尽了这里所有可能的排列

顺便说一下,这里是_getLocalFile

  Future<io.File> _getLocalFile(String filename) async
  {
    io.File f = new io.File(filename);
    return f;
  }
Future\u getLocalFile(字符串文件名)异步
{
io.File f=新的io.File(文件名);
返回f;
}

我认为您的
\u getLocalFile
函数返回了错误的数据类型。也许你可以尝试以下方法:

Future<File> _getLocalFile() async{
  final ImagePicker _picker = ImagePicker();
  PickedFile pickedFile= await _picker.getImage(source: ImageSource.gallery);
  File file =  File(pickedFile.path);
  return file;
}
因此,我认为您必须检查文件变量是否为null,如果为null,可能会显示一个按钮。如果用户按下按钮,可以调用async _getLocalFile函数,然后使用setState更新以显示图像

也许你也可以用FutureBuilder来包装这个图像:


    FutureBuilder<File>(
        future: _getLocalFile(),
        builder: (BuildContext context, AsyncSnapshot<File>  snapshot)
        {
          if(!snapshot.hasData){
            return CircularProgressIndicator();
          }else{
            return Container(
              decoration: new BoxDecoration(
                image: new DecorationImage(
                  fit: BoxFit.cover,
                  image: new FileImage(snapshot.data),
                ),
              ),
            );
          }
        }
    );

未来建设者(
未来:_getLocalFile(),
生成器:(BuildContext上下文,异步快照)
{
如果(!snapshot.hasData){
返回循环ProgressIndicator();
}否则{
返回容器(
装饰:新盒子装饰(
图片:新装饰图片(
适合:BoxFit.cover,
图像:新文件图像(snapshot.data),
),
),
);
}
}
);

我认为您的
\u getLocalFile
函数返回了错误的数据类型。也许你可以尝试以下方法:

Future<File> _getLocalFile() async{
  final ImagePicker _picker = ImagePicker();
  PickedFile pickedFile= await _picker.getImage(source: ImageSource.gallery);
  File file =  File(pickedFile.path);
  return file;
}
因此,我认为您必须检查文件变量是否为null,如果为null,可能会显示一个按钮。如果用户按下按钮,可以调用async _getLocalFile函数,然后使用setState更新以显示图像

也许你也可以用FutureBuilder来包装这个图像:


    FutureBuilder<File>(
        future: _getLocalFile(),
        builder: (BuildContext context, AsyncSnapshot<File>  snapshot)
        {
          if(!snapshot.hasData){
            return CircularProgressIndicator();
          }else{
            return Container(
              decoration: new BoxDecoration(
                image: new DecorationImage(
                  fit: BoxFit.cover,
                  image: new FileImage(snapshot.data),
                ),
              ),
            );
          }
        }
    );

未来建设者(
未来:_getLocalFile(),
生成器:(BuildContext上下文,异步快照)
{
如果(!snapshot.hasData){
返回循环ProgressIndicator();
}否则{
返回容器(
装饰:新盒子装饰(
图片:新装饰图片(
适合:BoxFit.cover,
图像:新文件图像(snapshot.data),
),
),
);
}
}
);

\u getLocalFile
中不需要任何未来,因为里面没有异步操作。你可以这么做

return Container( //<-- HEADER CONTAINER
    height: kHeaderHeight,
    width: kHeaderWidth,
    decoration:
    BoxDecoration(
        image: DecorationImage(
        image: _imageFileForHeader?.path != null
                ? Image.file(File(_imageFileForHeader.path)) 
                : Image.network(urlImage + _kHeaderImage);
        ),
    ),

您不需要在
\u getLocalFile
中有任何未来,因为其中没有异步操作。你可以这么做

return Container( //<-- HEADER CONTAINER
    height: kHeaderHeight,
    width: kHeaderWidth,
    decoration:
    BoxDecoration(
        image: DecorationImage(
        image: _imageFileForHeader?.path != null
                ? Image.file(File(_imageFileForHeader.path)) 
                : Image.network(urlImage + _kHeaderImage);
        ),
    ),

你能显示
\u getLocalFile
吗?我不确定你是否需要未来……你能展示一下
\u getLocalFile
?我不确定你是否需要未来…谢谢保罗。。。它看起来和我的函数很相似,不过还是让我试试吧..嗨,没问题。但我认为主要的问题是,你不能将FutureBuilder用作图像提供者:)也许如果你将FutureBuilder包装在容器上,它可以工作……谢谢Paul。。。它看起来和我的函数很相似,不过还是让我试试吧..嗨,没问题。但是我认为主要的问题是你不能将FutureBuilder用作图像提供者:)也许如果你将FutureBuilder包装在容器中,它就可以工作了……谢谢,但是有了这个Claudio,我得到了:getter'path'被调用为null。接收者:空尝试呼叫:路径谢谢。。路径错误是我的错。。但你是对的,保罗也是。我是grateful@houba:什么类型是
\u imageFileForHeader
?那是文件吗?我想我需要重新开始我在弗利特的课程。。。谢谢,先生_imageFileForHeader定义为:_imageFileForHeader=Wait picker.getImage(源:ImageSource.gallery);-->这不是我需要的。。我需要路径。谢谢,但是有了这个Claudio,我得到了:getter'path'被调用为null。接收者:空尝试呼叫:路径谢谢。。路径错误是我的错。。但你是对的,保罗也是。我是grateful@houba:什么类型是
\u imageFileForHeader
?那是文件吗?我想我需要重新开始我在弗利特的课程。。。谢谢,先生_imageFileForHeader定义为:_imageFileForHeader=Wait picker.getImage(源:ImageSource.gallery);-->这不是我需要的。。我需要那条路。
return Container( //<-- HEADER CONTAINER
    height: kHeaderHeight,
    width: kHeaderWidth,
    decoration:
    BoxDecoration(
        image: DecorationImage(
        image: _imageFileForHeader != null
                ? Image.file(_imageFileForHeader) 
                : Image.network(urlImage + _kHeaderImage);
        ),
    ),