大家好,我需要这方面的帮助,我正在尝试为我的注册用户添加个人资料图片并将个人资料图片保存在 firebase 中:
imported files here
class ProfilePage extends StatefulWidget {
@override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
final User? user = FirebaseAuth.instance.currentUser;
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
File? _imageFile;
String? _profileImageUrl;
@override
void initState() {
super.initState();
_loadProfileImage();
}
Future<void> _loadProfileImage() async {
if (user != null) {
DocumentSnapshot userDoc = await _firestore.collection('users').doc(user!.uid).get();
if (userDoc.exists && userDoc.data() != null) {
setState(() {
_profileImageUrl = userDoc['profilePic'];
});
}
}
}
Future<void> _pickImage() async {
final pickedFile = await ImagePicker().getImage(source: ImageSource.gallery);
if (pickedFile != null) {
setState(() {
_imageFile = File(pickedFile.path);
});
_uploadImage(_imageFile!);
}
}
Future<void> _uploadImage(File imageFile) async {
try {
String fileName = path.basename(imageFile.path);
Reference storageRef = FirebaseStorage.instance.ref().child('profile_pics/$fileName');
final UploadTask uploadTask = storageRef.putFile(imageFile);
final TaskSnapshot snapshot = await uploadTask.whenComplete(() => {});
final String downloadUrl = await snapshot.ref.getDownloadURL();
SharedPreferences prefs = await SharedPreferences.getInstance();
String? userId = prefs.getString('userId');
if (userId != null) {
await FirebaseFirestore.instance.collection('users').doc(userId).update({
'profilePic': downloadUrl,
});
setState(() {
_profileImageUrl = downloadUrl;
});
print('Profile picture uploaded successfully: $downloadUrl');
} else {
print('User ID not found in SharedPreferences');
}
} catch (e) {
print('Error uploading profile picture: $e');
}
}
Future<void> _signOut(BuildContext context) async {
await FirebaseAuth.instance.signOut();
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.remove('staySignedIn');
await prefs.remove('email');
await prefs.remove('password');
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => LoginPage()),
(Route<dynamic> route) => false,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(user?.displayName ?? 'Profile'),
actions: [
IconButton(
icon: Icon(Icons.logout),
onPressed: () => _signOut(context),
),
],
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
GestureDetector(
onTap: _pickImage,
child: CircleAvatar(
radius: 50,
backgroundImage: _imageFile != null
? FileImage(_imageFile!)
: _profileImageUrl != null
? NetworkImage(_profileImageUrl!) as ImageProvider
: null,
child: _imageFile == null && _profileImageUrl == null
? Icon(Icons.add_a_photo, size: 50)
: null,
),
),
SizedBox(height: 20),
Text(
user?.displayName ?? 'No Name',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
],
),
),
),
);
}
}
Firebase 存储似乎存在错误,但仅是由于此页面所致。 如果 somoene 可以帮助我,我将非常感激
我直接在 firebase 中尝试了我的收藏,看看问题是否真的来自那里,但效果很好。 我还尝试了其他帖子中提供的其他技术,但没有成功。
您可以直接上传图片,如下所示:
Reference storageRef = FirebaseStorage.instance.ref().child('profile_pics/$fileName');
await storageRef.putFile(imageFile);
String url = await storageRef.getDownloadURL();