how can i add data to List<> imgList if i pick image 2nd time then delete 1st data i add i try to use index to check is this can do ?
List<File?> files = [];
File? file;
List<ImageConfig> imgList = [];
Future<void> processImagePicker(ImageSource imageSource, int index) async {
try {
var result = await ImagePicker()
.pickImage(source: imageSource, maxWidth: 500, maxHeight: 700);
setState(() {
file = File(result!.path);
files[index] = file;
imgList.add(ImageConfig(
source: "picture", path: result.path, index: index.toString()));
});
for (var item in imgList) {
print('object ===> ${item.index}');
}
} catch (e) {
print(e);
}
}
class ImageCongfig is
class ImageConfig {
String? source;
String? path;
String? index;
ImageConfig({this.source, this.path, this.index});
}
CodePudding user response:
Just find item by using firstWhere method.
And remove that item from itemList.
void main() {
List<ImageConfig> datas = [
ImageConfig(source: '1', path: '1-2', index: '1-3'),
ImageConfig(source: '2', path: '2-2', index: '2-3'),
ImageConfig(source: '3', path: '3-2', index: '1-3'),
];
print(datas);
var foundItem = datas.firstWhere((item) => item.index == '1-3');
print('foundItem: $foundItem');
datas.remove(foundItem);
print(datas);
}
class ImageConfig {
String? source;
String? path;
String? index;
ImageConfig({this.source, this.path, this.index});
@override
String toString() => '''ImageConfig { source: $source,
path: $path,
index: $index}''';
}