为什么我得到这个表达式有一个'void'类型,所以它的值不能使用,在我的flutter应用程序上

yftpprvb  于 2022-12-14  发布在  Flutter
关注(0)|答案(2)|浏览(115)

我想使用flutter应用程序的改型返回图片,但在构建小部件上出现此错误:**此表达式的类型为“void”,因此无法使用其值。**这是我的代码:

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

我想返回getData()的内容。

jjjwad0x

jjjwad0x1#

不确定它是否有效,但也许你可以试试这个:

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

gg0vcinb2#

试试这个吧,我想这个对你有用:

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

相关问题