nzxcvbnm
nzxcvbnm

Reputation: 174

How to change Future Dynamic into String?

I am following old tutorial, and I get an error on ImageUrl in last line of code:

"Future dynamic can't be assigned to argument type String".

How can I fix that?

class _MyAppState extends State<MyApp> {

  static FirebaseStorage storage = FirebaseStorage(
    storageBucket: 'gs://natapp-7d2db/storage/natapp-7d2db.appspot.com/files'
  );

  static StorageReference imageRef = storage.ref().child('cake.png');

  final imageUrl = imageRef.getDownloadURL();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(brightness: Brightness.dark),
      home: Container(
        child: Image.network(imageUrl),
      ),
    );
  }
}

EDIT: I used

child: Image.network(imageUrl.toString)

Upvotes: 1

Views: 195

Answers (1)

Jitesh Mohite
Jitesh Mohite

Reputation: 34170

Replace Container with Use FutureBuilder, as FutureBuilder is a widget which used for an async callback which runs on Future

FutureBuilder<String>(
      future: imageRef.getDownloadURL(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
           return Container(
                child: Image.network(snapshot.data));
        }

        if (snapshot.hasError) return WidgetThatShowsError();

        // by default show progress because operation is async and we need to wait for result
        return CircularProgressIndicator();
      },
    );

Upvotes: 1

Related Questions