amin jamali
amin jamali

Reputation: 440

how to convert image url to asset in flutter?

i used multi_image_picker package to get images from gallery.now i want to implement edit page and i need to convert my images url list to asset and set the asset list to multi_image_picker. here is my multi image picker:

List<Asset> resultList = <Asset>[];
String error = 'No Error Detected';

try {
  resultList = await MultiImagePicker.pickImages(
    maxImages: 300,
    enableCamera: true,
    selectedAssets: _imagesAssets,
    cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
    materialOptions: MaterialOptions(
      actionBarColor: "#abcdef",
      actionBarTitle: "صندوق شهدای موردک",
      allViewTitle: "تمام تصاویر",
      useDetailsView: false,
      selectCircleStrokeColor: "#000000",
    ),
  );
} on Exception catch (e) {
  error = e.toString();
}

// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;

setState(() {
  _imagesAssets = resultList;
});

}

and a grid view to show the selected images:

 return GridView.count(
  crossAxisCount: 2,
  shrinkWrap: true,
  physics: NeverScrollableScrollPhysics(),
  padding: EdgeInsets.all(10),
  children: List.generate(_imagesAssets.length, (index) {
    Asset asset = _imagesAssets[index];
    return AssetThumb(
      asset: asset,
      width: 100,
      height: 100,
    );
  }),
);

Upvotes: 1

Views: 1859

Answers (2)

amin jamali
amin jamali

Reputation: 440

i just needed this function(i used uuid pacakge):

Future<Asset> fileToAsset(File image) async {
String fileName = basename(image.path);
var decodedImage = await decodeImageFromList(image.readAsBytesSync());
return Asset(uuid.v4(), fileName, decodedImage.width, decodedImage.height);

}

Upvotes: 0

raj kavadia
raj kavadia

Reputation: 1053

You cant put data in an asset since it's a file that is bundled and deployed with your app and is accessible at runtime. You can store it in a database or convert it to Uint8list.

Upvotes: 1

Related Questions