【发布时间】:2021-08-26 11:15:21
【问题描述】:
这有一个简单的 UI,带有一个 circleAvatar 和一个文本按钮。 onPressing texxt 按钮它应该打开 ImagePicker 并将所选图片上传到 firebase 并显示它在 circleAvatar 中的图像,但它不起作用,ImagePicker 工作正常但在 circleAvatar 和 Firestore 中没有显示图片。请求助,这里可以使用其他逻辑吗?
import 'package:flutter/material.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:image_picker/image_picker.dart';
class MyProfilePage extends StatefulWidget {
@override
_MyProfilePageState createState() => _MyProfilePageState();
}
class _MyProfilePageState extends State<MyProfilePage> {
String imageLink;
File _image;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: double.infinity,
child: Column(
children: <Widget>[
SizedBox(
height: 64,
), //just for spacing
imageLink != null
? CircleAvatar(
child: ClipOval(
child: Image.network(imageLink),
),
radius: 100,
)
: CircleAvatar(
child: ClipOval(
child: Icon(
Icons.person,
size: 100,
),
),
radius: 100,
),
SizedBox(
height: 16,
), //just for spacing
TextButton(
style: TextButton.styleFrom(backgroundColor: Colors.blueGrey),
child: Text("Change Image", style: TextStyle(color: Colors.yellow)),
onPressed: () async {
_image = (await ImagePicker()
.getImage(source: ImageSource.gallery)) as File;
FirebaseStorage fireStore = FirebaseStorage.instance;
Reference rootRef = fireStore.ref();
Reference pictureRef = rootRef.child("ProfilePictures");
pictureRef.putFile(_image).then((storageTask) async {
String link = await storageTask.ref.getDownloadURL();
print('Uploaded');
setState(() {
imageLink = link;
});
});
},
)
],
),
));
} }
【问题讨论】:
-
如果对您有用,请选择已接受的答案
标签: flutter dart firebase-storage