如何将 'Future<String>' 转换为 'String'

How to convert 'Future<String>' to the 'String'

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

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');
url = await ref.getDownloadURL();
}    

然后在 Widget _buildlistItem 字段里面:

showImage(); //called here like this.

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

  Image.network( url,),