[英]Displaying image in container using Firebase storage in Flutter?
I'm new to Firebase and I don't know how to move forward from here now.我是 Firebase 的新手,我现在不知道如何从这里继续前进。 I have an image in Firebase storage and I just want to display it in my app.我在 Firebase 存储中有一张图像,我只想在我的应用程序中显示它。 In full size, so I chose Container.全尺寸,所以我选择了Container。 Now by watching a tutorial I have wrote some code but didn't get anywhere.现在通过观看教程,我编写了一些代码,但没有得到任何结果。 Please can you guys help.请大家帮忙。 It maybe a silly question but you know the beginners.这可能是一个愚蠢的问题,但你知道初学者。 Thank you in advance.先感谢您。 I've updated by code with StreamBuilder.我已经用 StreamBuilder 的代码进行了更新。
**MAIN.DART**
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: DetailScreen(),
);
}
}
**DETAILSCREEN.DART
class DetailScreen extends StatefulWidget {
@override
_DetailScreenState createState() => _DetailScreenState();
}
class _DetailScreenState extends State<DetailScreen> {
StreamSubscription<QuerySnapshot> subscription;
List<DocumentSnapshot> wallpaperList;
final CollectionReference collectionReference =
FirebaseFirestore.instance.collection("wallpaperimg");
@override
void initState() {
super.initState();
subscription = collectionReference.snapshots().listen((datasnapshot) {
setState(() {
wallpaperList = datasnapshot.docs;
});
});
}
@override
void dispose() {
subscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return return StreamBuilder(
stream: FirebaseFirestore.instance
.collection("wallpaperimg")
.doc()
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text("Loading");
}
var profiledetail = snapshot.data;
return Container(
child: Image(image: NetworkImage(profiledetail['imgUrl'])));
});
}
}
This is assuming that every document in firebase is storing a single imageUrl.这是假设 firebase 中的每个文档都存储一个 imageUrl。
To display the first image in your list, you can use:要显示列表中的第一张图片,您可以使用:
Container(
child: Image.network(wallpaperList[0].data()['imageUrl'])
)
But since you are retrieving multiple documents, not just one, you can use a ListView.builder to do that to display all the images you are getting.但是由于您要检索多个文档,而不仅仅是一个,您可以使用 ListView.builder 来显示您获得的所有图像。
ListView.builder(
itemCount: wallpaperList.length,
itemBuilder: (context, index) {
return Container(
child: Image.network(wallpaperList[index].data()['imageUrl'])
);})
Post how your data looks in firebase, to get a better answer.在 firebase 中发布您的数据的外观,以获得更好的答案。
-- edit 2: Since youa re using streambuilder now, you need to pass the the document id to your query .collection('wallpaperimg').doc('theRandomNumberDocumentIdinfirebase').get()
-- 编辑 2:由于您现在正在使用 streambuilder,您需要将文档 ID 传递给您的查询.collection('wallpaperimg').doc('theRandomNumberDocumentIdinfirebase').get()
use like this pass your stream where your images is saved.像这样使用保存图像的 stream 。
StreamBuilder(流生成器(
stream: Firestore.instance
.collection("userprofile")
.document(userid)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text("Loading");
} var Profiledetail = snapshot.data;return Container(child:Image(image:NetworkImage(Profiledetail["imgurl"])));
I hope this would be Helpful我希望这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.