Home > OS >  Flutter _TypeError (type 'Future<String>' is not a subtype of type 'Future<W
Flutter _TypeError (type 'Future<String>' is not a subtype of type 'Future<W

Time:04-04

There is a photo like this in Firebase Storage:

enter image description here

I want to place this photo in a column. I wrote a code like this:

class MainScreen extends StatefulWidget {
      @override
      State<MainScreen> createState() => _MainScreenState();
    }
    class _MainScreenState extends State<MainScreen> {
      @override
      Widget build(BuildContext context) {
        return Column(
          children: <Widget>[
            Container(
              height: 200,
              width: double.infinity,
              decoration: BoxDecoration(
                color: Colors.blueAccent,
                borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(30),
                  bottomRight: Radius.circular(30),
                ),
              ),
              child: Column(
                children: [
                  FutureBuilder<Widget>(
                    future: downloadURLExample(),
                    builder: (BuildContext context, AsyncSnapshot<Widget> snapshot) {
                    if(snapshot.hasData)
                      return snapshot.data;
                    }
                  ),
                  SizedBox(height: 10),
                  Text(
                    "Hello",
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    ),
                  ),
                ],
              ),
            ),
          ] 
        );
      }
    }
  Future<void> downloadURLExample() {
    var downloadURL = FirebaseStorage.instance.ref('defaultProfilePhoto').getDownloadURL();
    return downloadURL;
  }

When I run this code, I get an error like this:

enter image description here

_TypeError (type 'Future<String>' is not a subtype of type 'Future<Widget>?')

How can I solve this problem? Thanks in advance for the help.

I've looked at other threads but no solution.

CodePudding user response:

You are getting this error because you are returning Future<String> from _downloadURLExample method but future property of your FutureBuilder widget is having type of Future<Widget> and hence you are getting type mismatch error.

Use this code

FutureBuilder<String>(
                future: downloadURLExample(),
                builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
                if(snapshot.hasData)
                  return Image.network(snapshot.data);
                }
              ),


Future<String> downloadURLExample() {
    var downloadURL = FirebaseStorage.instance.ref('defaultProfilePhoto').getDownloadURL();
    return downloadURL;
  }

CodePudding user response:

I'm assuming you want to show the image in your UI.

Try this:

  Future<Widget> downloadURLExample() async {
    var downloadURL = await FirebaseStorage.instance.ref('defaultProfilePhoto').getDownloadURL();
    return Image.network(downloadURL);
  }
  • Related