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.
CodePudding user response:
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();
}
}
);
}
}
CodePudding user response:
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();
}
}
);
}
}