简体   繁体   English

API 数据显示 - flutter 卡片小部件

[英]API data display - flutter card widget

Get API connection is successful.获取API 连接成功。 but I have no idea how to map data.. how is connect snapshot.data, I want to display data in a Stack widget.但我不知道如何 map 数据.. 如何连接 snapshot.data,我想在 Stack 小部件中显示数据。

https://flutter.dev/docs/cookbook/networking/fetch-data https://flutter.dev/docs/cookbook/networking/fetch-data

I learned this from the association with this source我从与此来源的关联中了解到这一点

Future<News> fetchNews() async {
  final response =
  await http.get('#url');
  print(response.body);

  if (response.statusCode == 200) {
    return News.fromJson(jsonDecode(response.body));
  } else {
    throw Exception('Failed to load news');
  }
}

class News {
  final String title;
  final String description;
  final String image;

  News({this.title, this.description, this.image});

  factory News.fromJson(Map<String, dynamic> json) {
    return News(
      title: json['title'],
      description: json['description'],
      image: json['image'],
    );
  }
}

  Widget _buildPage(BuildContext context, int direction) {
    Size size = MediaQuery.of(context).size;
    return SafeArea(
      child: FutureBuilder<News>(
        future: futureNews,
          builder: (context, snapshot) {
          if (snapshot.hasData) {
            return newsCard(snapshot.data);
          } else if (snapshot.hasError) {
            return Text("${snapshot.error}");
          }
          return CircularProgressIndicator();
      },
    ),
    );
  }

  Widget newsCard(size){
    return Stack(
      children: <Widget>[
        Container(
          child: Image.network(
              image,
              fit: BoxFit.cover
          ),
          height: size.height/2.0,
        ),
        ),
      ],
    );
  }

Your code is a bit messy and it doesn't make much sense.你的代码有点乱,没有多大意义。 It seems obvious that the data is not getting displayed because you're passing your News object to your newsCard function, but the parameter seems to be expecting Size .很明显,数据没有显示,因为您将News object 传递给您的newsCard function,但参数似乎是Size I also don't know where are you getting the image property from since it doesn't seem to be declared anywhere in the code.我也不知道你从哪里得到image属性,因为它似乎没有在代码中的任何地方声明。

Doing some changes to your code, a minimal working function that returns your widget would look like this:对您的代码进行一些更改,返回您的小部件的最小工作 function 将如下所示:


Widget _buildPage(BuildContext context, int direction) {
    Size size = MediaQuery.of(context).size;
    return SafeArea(
      child: FutureBuilder<News>(
        future: futureNews,
          builder: (context, snapshot) {
          if (snapshot.hasData) {
            return newsCard(snapshot.data, size);
          } else if (snapshot.hasError) {
            return Text("${snapshot.error}");
          }
          return CircularProgressIndicator();
      },
    ),
    );
  }

  Widget newsCard(News news, Size size){
    return Stack(
      children: <Widget>[
        Container(
          child: Image.network(
              news.image,
              fit: BoxFit.cover
          ),
          height: size.height/2.0,
        ),
        ),
      ],
    );
  }


UPDATE更新

If you're getting the error you mentioned bellow, is likely that your response from the get request is a list of News and not just one News object, therefore you'll have to change this:如果您收到您在下面提到的错误,很可能您对获取请求的响应是News列表,而不仅仅是一个News object,因此您必须更改此:

return News.fromJson(jsonDecode(response.body));

To this:对此:

return jsonDecode(response.body).map<News>((object) => News.fromJson(object)).toList();

The return type of fetchNews() should be Future<List<News>> instead of Future<News> fetchNews()的返回类型应该是Future<List<News>>而不是Future<News>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM