1

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)
nzxcvbnm
  • 154
  • 1
  • 10
  • 1
    Does this answer your question? [What is a Future and how do I use it?](https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it) – nvoigt Jul 29 '20 at 14:12

1 Answers1

1

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();
      },
    );
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147