Search code examples
fluttergetwidgetretrofitdisplay

why I got This expression has a type of 'void' so its value can't be used. on my flutter application


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.


Solution

  • 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();
            }
          }
        );
      }
    }