首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在get请求之后,数据不会显示在我的屏幕上

在get请求之后,数据不会显示在我的屏幕上
EN

Stack Overflow用户
提问于 2022-02-11 15:54:33
回答 1查看 283关注 0票数 0

在此代码中,数据成功地添加到我的“项”列表中。但是在第二个代码中,当我调用‘items’列表时,它看起来是空的。我应该做什么来显示数据?

代码语言:javascript
复制
import 'dart:convert';

import 'package:http/http.dart' as http;

class IceCream {
  final String id;
  final String? title;
  final String? shortTitle;
  final String? description;
  final String? longDescription;
  final double price;
  final String imageUrl;
  final double? kilos;
  final double? review;
  final int? reviewCount;

  IceCream(
      {required this.id,
      this.title,
      this.shortTitle,
      this.description,
      this.longDescription,
      required this.price,
      required this.imageUrl,
      this.kilos,
      this.review,
      this.reviewCount});
}

class Products {
  List<IceCream> _items = [];

  List<IceCream> get items {
    return [..._items];
  }

  Future<void> fetchProducts() async {
    final url = Uri.parse(
        'https://ice-cream-app-31dce-default-rtdb.europe-west1.firebasedatabase.app/icecreams.json');

    try {
      final response = await http.get(url);
      //    print(response.body);
      final extractedData = json.decode(response.body) as Map<String, dynamic>;
      if (extractedData.length == null) {
        return;
      }
      final List<IceCream> loadedProduct = [];
      extractedData.forEach((prodId, prodData) {
        loadedProduct.add(IceCream(
            id: prodId,
            title: prodData['title'],
            description: prodData['description'],
            price: prodData['price'],
            imageUrl: prodData['imageUrl'],
            shortTitle: prodData['shortTitle'],
            longDescription: prodData['longDescription'],
            kilos: prodData['kilos'],
            review: prodData['review'],
            reviewCount: prodData['reviewCount']));
      });

      _items = loadedProduct;
      //print(items);
    } catch (error) {
      print(error);
      throw (error);
    }
  }
}

第二个代码:

代码语言:javascript
复制
import 'package:flutter/material.dart';
import 'package:ice_cream_ui_clone/model/ice_cream.dart';
import 'package:ice_cream_ui_clone/screens/detail_screen.dart';

import '../utils/constants.dart';
import 'add_item_button.dart';

class TopFlavoursList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final cart = Products().items;

    return Expanded(
      flex: 2,
      child: ListView.builder(
          itemCount: cart.length,
          scrollDirection: Axis.horizontal,
          itemBuilder: (ctx, i) => GestureDetector(
                onTap: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (ctx) => DetailScreen(
                                item: cart[i],
                              )));
                },
                child: Card(
                  color: myLightPinkColor,
                  child: Padding(
                      padding: EdgeInsets.all(lowPadding),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Image.network(
                            cart[i].imageUrl,
                            height: midImageHeight,
                            width: midImageWidht,
                          ),
                          SingleChildScrollView(
                            child: Column(
                              children: [
                                Text(cart[i].title!,
                                    style:
                                        Theme.of(context).textTheme.headline5),
                                SizedBox(
                                  height: lowPadding,
                                ),
                                Row(
                                  children: [
                                    Text("${cart[i].kilos.toString()} KG",
                                        style: Theme.of(context)
                                            .textTheme
                                            .headline6),
                                    SizedBox(
                                      width: lowImageWidht,
                                    ),
                                    Icon(
                                      Icons.star,
                                      color: myYellowColor,
                                    ),
                                    Text(cart[i].review.toString(),
                                        style: Theme.of(context)
                                            .textTheme
                                            .headline6)
                                  ],
                                ),
                                Row(
                                  children: [
                                    Text("\$${cart[i].price.toString()}",
                                        style: Theme.of(context)
                                            .textTheme
                                            .headline5),
                                    SizedBox(
                                      width: lowImageWidht,
                                    ),
                                    AddItemButton(
                                      buttonShape: const CircleBorder(),
                                    )
                                  ],
                                )
                              ],
                            ),
                          )
                        ],
                      )),
                ),
              )),
    );
  }
}
EN

Stack Overflow用户

发布于 2022-02-11 16:02:17

我认为问题在于"forEach“的地位。应该更像是

代码语言:javascript
复制
extractedData.forEach((data) {
  loadedProduct.add(IceCream())
});

forEach中的函数应该使用一个参数,而不是2

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71082962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档