I'm pretty new to flutter and really, really want to use provider to pass data around, but I just can't get it to work. I think I have everything set right, but for some reason when I notify listeners, my app doesn't refresh. All I'm trying to do is update a profile pic. I start with a pic that is in firebase (works fine). Then I give the user the option to update their pic with camera or photos. When they choose a new pic, the consumer doesn't update like I would expect it to. Can someone please tell me if I'm doing something wrong or how I may be able to accomplish this?
First, here is my main:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<Storage>(create: (context) => Storage()),
StreamProvider<Uid>.value(value: AuthService().user),
],
child: MaterialApp( ....
Then here is the class and method that updates the photo URL which I call from an image picker class. I'm passing context so I can pop that image picker view and go back to the profile page after it's done.
class Storage with ChangeNotifier {
String userPhotoUrl;
void uploadImage(PickedFile _imageFile, BuildContext context) async {
.... //some other code here
try {
var snapshot =
await _storage.ref().child('/profileImages/$_userId').putFile(file);
await snapshot.ref.getDownloadURL().then((value) {
userPhotoUrl = value;
notifyListeners();
print(userPhotoUrl); //I know the code is working because it prints here
Navigator.pop(context);
});
} catch (e) {
print(e);
}
}
}
And here is the profile page that I'm trying to update. It's just not getting the updated value.
class Profile extends StatefulWidget {
Profile({Key key}) : super(key: key);
@override
_ProfileState createState() => _ProfileState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _ProfileState extends State<Profile> with TickerProviderStateMixin {
String imageUrl; //firebase pic to start - works fine
@override
Widget build(BuildContext context) {
....//some more widget tree
Consumer<Storage>(
builder: (context, storage, widget) =>
CircularProfileAvatar( //never updates. userPhotoURL always null
storage.userPhotoUrl ?? imageUrl, //always uses imageUrl,
elevation: 20,
borderColor: Colors.white70,
borderWidth: 1,
radius: 50,
),
),
Any idea as to why it's not working? I would think this should be easy but provider is still alluding me...