skip to Main Content

I want to return a picture using retrofit for my flutter application but on the build Widget I got this error : **This expression has a type of ‘void’ so its value can’t be used.
**
this is my code:

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  Future<void> getData() async {
    final dio = Dio();
    final client = ApiService(dio);
    final response = await client.downloadFile();
    WidgetsFlutterBinding.ensureInitialized();

    runApp(MaterialApp(
      home: Scaffold(
        body: Center(
          child: Image.memory(response.response.data),
        ),
      ),
    ));
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<void>(
      future: getData(),
      builder: (context, AsyncSnapshot<void> snapshot) {
        if (snapshot.hasData) {
          return Text(snapshot.data);
        } else {
          return CircularProgressIndicator();
        }
      }
    );
  }
}


I want to return the getData() content.

2

Answers


  1. Not sure if it works but maybe you can try this:

    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      Future<Image> getData() async {
        final dio = Dio();
        final client = ApiService(dio);
        final response = await client.downloadFile();
        return Image.memory(response.response.data);
      }
    
      @override
      Widget build(BuildContext context) {
        return FutureBuilder<Image>(
          future: getData(),
          builder: (context, AsyncSnapshot<Image> snapshot) {
            if (snapshot.hasData) {
              return snapshot.data;
            } else {
              return CircularProgressIndicator();
            }
          }
        );
      }
    }
    
    Login or Signup to reply.
  2. Try this once I think this will work for you:

    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
     Future<dynamic> getData() async {
        final dio = Dio();
        final client = ApiService(dio);
        final response = await client.downloadFile();
        return response.response.data;
      }
    
      @override
      Widget build(BuildContext context) {
        return FutureBuilder<dynamic>(
          future: getData(),
          builder: (context, AsyncSnapshot<dynamic> snapshot) {
            if (snapshot.hasData) {
              return Image.memory(snapshot.data);
            } else {
              return CircularProgressIndicator();
            }
          }
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search