【问题标题】:How to convert 'Future<String>' to the 'String'如何将 'Future<String>' 转换为 'String'
【发布时间】:2021-07-26 08:24:38
【问题描述】:

我正在尝试显示 firestore 图像,但我使用它的代码需要 Future,但在表中显然我不能使用它。我能以某种方式把它变成字符串吗?这是获取链接的地方:

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]),
            );
          }),
    );
  }
}

【问题讨论】:

  • 我自己解决了!!!我将该字段做成这样的函数:void showImage() async { final ref = FirebaseStorage.instance.ref().child('erkekyuz.png'); // no need of the file extension, the name will do fine. url = await ref.getDownloadURL(); } showImage(); //called here like this.
    然后在 Image 字段中像这样:`Image.network(url,),`
  • 很高兴听到 Crag ????。我建议将其发布为自我回答并接受它,以便人们和系统都知道您的问题已得到回答。

标签: firebase flutter google-cloud-firestore firebase-storage


【解决方案1】:

我自己解决了!!!我把那个字段变成了这样的函数:

void showImage() async {  
final ref = FirebaseStorage.instance.ref().child('erkekyuz.png');
url = await ref.getDownloadURL();
}    

然后在 Widget _buildlistItem 字段内:

showImage(); //called here like this.

然后在像这样的图像字段:

  Image.network( url,),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 2020-05-20
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多