Home > Blockchain >  How to fix error "The argument type 'Object?' can't be assigned to the parameter
How to fix error "The argument type 'Object?' can't be assigned to the parameter

Time:06-21

i am trying to retrieve image from firebase storage. i was following youtube tutorial and the error was on the snapshot.data saying The argument type 'Object?' can't be assigned to the parameter type 'Widget?'. please help me to fix this error. thank you so much


final String image1 = "images/image0.png";
final String image2 = "images/image0.png";

String image = image1;

class LoadFirebaseStorageImage extends StatefulWidget {
  const LoadFirebaseStorageImage({Key? key}) : super(key: key); ```

            Container(
              height: double.infinity,
              margin: const EdgeInsets.only(left: 30, right: 30, top: 10),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(30),
                child: FutureBuilder(
                  future: _getImage(context, image),
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.done)
                      return Container(
                          height: MediaQuery.of(context).size.height / 1.25,
                          width: MediaQuery.of(context).size.width / 1.25,
                          child: snapshot.data);
                    if (snapshot.connectionState == ConnectionState.waiting)
                      return Container(
                          height: MediaQuery.of(context).size.height / 1.25,
                          width: MediaQuery.of(context).size.width / 1.25,
                          child: CircularProgressIndicator());
                    return Container();
                  },
                ),
              ),
            ),
          ],
        ),
      ),
      loadButton(context),
    ],
  ),
);

} }```

CodePudding user response:

you need to indicate the type of the snapshot at the FutureBuilder, like

FutureBuilder<Widget>(

This is assuming that _getImage returns a Widget

CodePudding user response:

You are getting this error because you have parsed snapshot.data directly to the widget. You can not pass the data directly to the widget. first, check what are you getting on snapshot.data by printing it. if you are getting the network URL of the image then you can use it like below.

you will always get some data not widget in snapshot.data. just check what are you getting print function.

       Container(
          height: double.infinity,
          margin: const EdgeInsets.only(left: 30, right: 30, top: 10),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(30),
            child: FutureBuilder(
              future: _getImage(context, image),
              builder: (context, snapshot) {
                print(snapshot.data);

                if (snapshot.connectionState == ConnectionState.done)
                  return Container(
                      height: MediaQuery.of(context).size.height / 1.25,
                      width: MediaQuery.of(context).size.width / 1.25,
                      child: Image.network(snapshot.data));
                if (snapshot.connectionState == ConnectionState.waiting)
                  return Container(
                      height: MediaQuery.of(context).size.height / 1.25,
                      width: MediaQuery.of(context).size.width / 1.25,
                      child: CircularProgressIndicator());
                return Container();
              },
            ),
          ),
        ),
      ],
    ),
  ),
  • Related