'Flutter Provider Consumer Doesn't Update With Change / Image Picker with provider
im trying to make my own image/file picker by using photo manager package and provider package. I was able to successfully take photos and videos in the device gallery and create thumbnails. I covered the thumbnails with gestureDetector and add the clicked asset to the provider state in Uint8List format. I made consumer transactions where the added images will be displayed, but the consumer does not update on change. Here my codes;
child: MultiProvider(
providers: [
ChangeNotifierProvider(create: (BuildContext context) => HoldPickedAsset()),
],
builder: (context, Widget){
return GestureDetector(
onTap: (){
var insert = Provider.of<HoldPickedAsset>(context, listen: false);
if(asset.type == AssetType.image){
insert..insertToLists(Posts(type: "1", urlPath: snapshot.data));
}else if(asset.type == AssetType.video){
insert..insertToLists(Posts(type: "3", urlPath: snapshot.data));
}else if(asset.type == AssetType.audio){
insert..insertToLists(Posts(type: "4", urlPath: snapshot.data));
}
},
child: Image.memory(
snapshot.data!,
fit: BoxFit.cover,
),
);
}
),
and my modal;
class HoldPickedAsset extends ChangeNotifier{
var postStates = [];
insertToLists(pos.Posts posts){
postStates.insert(0, posts);
notifyListeners();
}
removeFromLists(index){
postStates.remove(index);
notifyListeners();
}
}
and lastly the consumer part;
MultiProvider(
providers: [
ChangeNotifierProvider<HoldPickedAsset>(create: (BuildContext context) => HoldPickedAsset()),
],
child: Consumer<HoldPickedAsset>(
builder: (context, state, widget) {
var lastPost = state.postStates;
print(lastPost);
}
)
),
when I run this code, it just prints an empty list to debug console once but doesn't print anything again on change... Thanks in advance for your help :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
