On the profile page of my app I have an ImagePicker being called when the user selects the circular avatar image. The problem is that when the user selects the image, it isn’t displayed on the page. However, when the user saves the profile, the image is saved in both the database (under the user) and firebase storage. It even displays in real time if the user leaves and comes back. I can’t figure out why the initial ImagePicker doesn’t display the image.
Here is the code where the image is being displayed on the profile page:
...
child: Column(
children: [
Container(
padding: const EdgeInsets.all(2),
decoration: BoxDecoration(
border: Border.all(
color: Colors.greenAccent.shade400, width: 2),
borderRadius: BorderRadius.circular(70),
),
child: userData['profilePic'] == ''
? CircleAvatar(
radius: 56,
backgroundColor: Colors.grey.shade900,
child: const Icon(
Icons.camera_alt,
color: Colors.white,
size: 50,
),
)
: CircleAvatar(
radius: 56,
backgroundColor: Colors.white,
backgroundImage:
NetworkImage(userData['profilePic']))),
Here is the ImagePicker Code:
// Image Picker
imagePickDialog() {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Center(
child: Text('Upload Image',
style: GoogleFonts.knewave(color: Colors.black))),
content: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: () async {
final ImagePicker _picker = ImagePicker();
final XFile? image =
await _picker.pickImage(source: ImageSource.camera);
if (image != null) {
profileImage = File(image.path);
setState(() {});
Navigator.pop(context);
}
},
child: Icon(
Icons.camera_alt,
size: 40,
color: Colors.greenAccent.shade400,
),
),
const SizedBox(
width: 30,
),
InkWell(
onTap: () async {
final ImagePicker _picker = ImagePicker();
final XFile? image = await _picker.pickImage(
source: ImageSource.gallery,
);
if (image != null) {
profileImage = File(image.path);
setState(() {});
Navigator.pop(context);
}
},
child: Icon(Icons.photo_library_rounded,
size: 40, color: Colors.greenAccent.shade400),
),
],
),
);
},
);
}
// Profile Pic File Image
File? profileImage;
// Data Controller
late DataController? dataController;
// Init State
@override
void initState() {
// init data controller
super.initState();
// fetch data from myDocument from Data Controller
dataController = Get.put(DataController());
try {
profileImage = dataController!.myDocument!.get('profilePic');
} catch (e) {
profileImage = null;
}
}
I first tried:
profileImage == null
? CircleAvatar(
radius: 56,
backgroundColor: Colors.grey.shade900,
child: const Icon(
Icons.camera_alt,
color: Colors.white,
size: 50,
),
)
: CircleAvatar(
radius: 56,
backgroundColor: Colors.white,
backgroundImage: FileImage(
profileImage!,
),
),
That will display the image from the ImagePicker but it only saves it locally.
Then I tried:
userData['profilePic'] == ''
? CircleAvatar(
radius: 56,
backgroundColor: Colors.grey.shade900,
child: const Icon(
Icons.camera_alt,
color: Colors.white,
size: 50,
),
)
: CircleAvatar(
radius: 56,
backgroundColor: Colors.white,
backgroundImage: NetworkImage(
userData['profilePic'],
),
),
That saves the image to the database, and will display in real time for the user but the ImagePicker doesn’t work.
What am I doing wrong?
2
Answers
Got it to work!