Firebase 如何转换';未来<;字符串>';到';字符串';
我试图显示firestore图像,但我使用它的代码需要将来使用,但在表中,显然我不能使用它。我能把它变成绳子吗?这是进行链接的地方:Firebase 如何转换';未来<;字符串>';到';字符串';,firebase,flutter,google-cloud-firestore,firebase-storage,Firebase,Flutter,Google Cloud Firestore,Firebase Storage,我试图显示firestore图像,但我使用它的代码需要将来使用,但在表中,显然我不能使用它。我能把它变成绳子吗?这是进行链接的地方: final ref = FirebaseStorage.instance.ref().child('erkekyuz'); // no need of the file extension, the name will do fine. var url = ref.getDownloadURL(); 在我调用它的地方: Text(url), //THIS I
final ref = FirebaseStorage.instance.ref().child('erkekyuz');
// no need of the file extension, the name will do fine.
var url = ref.getDownloadURL();
在我调用它的地方:
Text(url), //THIS IS WHERE IT'S USED, only this doesn't work, if I remove this row, everything else works.
这是完整的代码。请随意使用它,它完全可以在没有图像的情况下工作,如果我删除这一行,它可以完美地工作,如果您只需要文本数据:
import 'package:flutter/material.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Basic Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'new Flutter App'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Widget _buildlistItem(BuildContext context, DocumentSnapshot document) {
final ref = FirebaseStorage.instance.ref().child('erkekyuz');
// no need of the file extension, the name will do fine.
var url = ref.getDownloadURL();
String mezunD;
if (document.data()['mezunDurumu'] == false) {
mezunD = "mezun değil";
}
if (document.data()['mezunDurumu'] == true) {
mezunD = "mezun";
}
String yasString = (document.data()['yas']).toString();
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Table(
border: TableBorder.all(),
children: [
TableRow(children: [
Text("Ad Soyad: "),
Text(document.data()['adSoyad']),
]),
TableRow(children: [
Text("Yaş: "),
Text(yasString),
]),
TableRow(children: [
Text("Doğum Tarihi: "),
Text(document.data()['dogumTarihi']),
]),
TableRow(children: [
Text("Mezun Durumu: "),
Text(mezunD),
]),
TableRow(children: [
Text("Fotoğraf: "),
Image(url), //THIS IS WHERE IT'S USED, only this doesn't work, if I remove this row, everything else works.
]),
],
),
);
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Öğrenci Durumu"),
),
body: StreamBuilder(
stream: FirebaseFirestore.instance.collection('tablolar').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return const Text('Loading...');
return ListView.builder(
itemExtent: 100.0,
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index) =>
_buildlistItem(context, snapshot.data.docs[index]),
);
}),
);
}
}
导入“包装:颤振/材料.省道”;
导入“包:firebase_存储/firebase_存储.dart”;
导入“包:cloud_firestore/cloud_firestore.dart”;
导入“包:firebase_core/firebase_core.dart”;
Future main()异步{
WidgetsFlutterBinding.ensureInitialized();
等待Firebase.initializeApp();
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振基本演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:我的主页(标题:“新颤振应用程序”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
小部件_buildlistItem(BuildContext上下文,DocumentSnapshot文档){
final ref=FirebaseStorage.instance.ref().child('erkekyuz');
//不需要文件扩展名,名称就可以了。
var url=ref.getDownloadURL();
弦乐;
if(document.data()['mezundrumu']==false){
mezunD=“mezun değil”;
}
if(document.data()['mezundrumu']==true){
mezunD=“mezun”;
}
字符串yastring=(document.data()['yas']).toString();
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:表格(
边框:TableBorder.all(),
儿童:[
TableRow(儿童:[
正文(“Ad Soyad:”),
文本(document.data()['adSoyad']),
]),
TableRow(儿童:[
文本(“雅:”),
文本(字符串),
]),
TableRow(儿童:[
文本(“Doğum Tarihi:”),
文本(document.data()['dogumTarihi']),
]),
TableRow(儿童:[
文本(“Mezun Durumu:”),
文本(mezunD),
]),
TableRow(儿童:[
文本(“Fotoğraf:”),
Image(url),//这是它的使用位置,只有这一行不起作用,如果我删除这一行,其他一切都起作用。
]),
],
),
);
}
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“ğrenci Durumu”),
),
正文:StreamBuilder(
流:FirebaseFirestore.instance.collection('tablolar').snapshots(),
生成器:(上下文,快照){
如果(!snapshot.hasData)返回常量文本('Loading…');
返回ListView.builder(
项目范围:100.0,
itemCount:snapshot.data.docs.length,
itemBuilder:(上下文,索引)=>
_buildlistItem(上下文、快照、数据、文档[索引]),
);
}),
);
}
}
我自己刚解决了!!!我将该字段转换为如下函数:
void showImage() async {
final ref = FirebaseStorage.instance.ref().child('erkekyuz.png');
url = await ref.getDownloadURL();
}
Image.network( url,),
然后在Widget\u buildlistItem字段中:
showImage(); //called here like this.
然后在图像字段处,如下所示:
void showImage() async {
final ref = FirebaseStorage.instance.ref().child('erkekyuz.png');
url = await ref.getDownloadURL();
}
Image.network( url,),
我自己解决了!!!我将该字段设置为如下函数:
void showImage()async{final ref=firebasetory.instance.ref().child('erkekyuz.png');//不需要文件扩展名,名称就可以了。url=wait ref.getDownloadURL();}showImage()//像这样在这里调用。
然后在图像字段中,像这样:`Image.network(url,),`Good to hear Crag