提供者ImagePicker

时间:2020-07-01 09:48:05

标签: flutter provider imagepicker

从图库中选取图像时,提供程序出现问题,该图像没有出现在UI中。

我尝试改用Text,它确实运行良好。我认为问题出在futureBuilder之类的东西上

class ImageController extends ChangeNotifier {
   Future<File> _image;

   pickImageFromGallery(ImageSource source) {
       _image = ImagePicker.pickImage(source: source);
       NotificationListener();
   }

  Widget imageFromGallery() {

    return  FutureBuilder<File>(
     future: _image,
     builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
       if (snapshot.connectionState == ConnectionState.done &&
           null != snapshot.data) {
         Utility.saveImageToPreferences(
             Utility.base64String(snapshot.data.readAsBytesSync()));
         return
          Image.file(
           snapshot.data,

         );
       } else if (null != snapshot.error) {
         return const Text(
           'Error Picking Image',
           textAlign: TextAlign.center,
         );
       } else {
         return const Text(
           'No Image Selected',
           textAlign: TextAlign.center,
         );
       }
      },
     );
  }
}

这就是我从另一个widgit显示它的方式

Consumer<ImageController>(
                    builder: (context, image,child){
                    return  Provider.of<ImageController>(context).imageFromGallery();
                    },
                  ),

1 个答案:

答案 0 :(得分:0)

使用者已经检索到ImageControllerConsumer是用构建器编写Provider.of并返回小部件的更好方法。

Consumer<ImageController>(
  builder: (context, imageController, child){
    return  imageController.imageFromGallery();
  },
),