首页 > 解决方案 > 从具有 n:m 关系的网络中获取数据并将其存储为模型类并在颤动的每一侧存储参考的正确方法是什么?

问题描述

我花了几天时间思考和搜索如何实现这一目标,但仍然不知道它实际上是如何工作的……如果有人能给我一些建议或指出正确的方向,我将不胜感激。

所以在我的例子中,我将从 API 中获取一个 json 列表,我只想将Shop类存储到一个列表中,该列表将构建在一个listView.

这是我的代码示例。

Widget buildShopList() {
  return ListView.builder(
    itemCount: shopList.length,
    padding: const EdgeInsets.only(top: 8, bottom: 110),
    scrollDirection: Axis.vertical,
    itemBuilder: (BuildContext context, int index) {
      final int count = shopList.length > 10 ? 10 : shopList.length;
      return ShopListView(
        callback: () {},
        shop: shopList[index],
      );
    },
  );
}

class ShopListView extends StatelessWidget {
  const ShopListView({Key key, this.shop}: super(key: key);

  final Shop shop;

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text(shop.type.name),
        Text(shop.name),
      ]
    );
  }
}

店铺模型类

class Shop {
  String name;
  String website;
  String email;
  ShopType type;

  Shop({this.name, this.website, this.email, this.type});

  factory Shop.fromJson(Map<String, dynamic> json) => Shop(
    name: json["name"],
    type: json["shop_types"],
    // can't figure out how to pass the shop type model class into here
    website: json["website"],
    email: json["email"],
  );

  Map<String, dynamic> toJson() => {
    "name": name,
    "shop_types": type,
    "website": website,
    "email": email
  };
}

ShopType 模型类

class ShopType {
  int id;
  String name;
  String description;

  ShopType({this.id, this.name, this.description});

  factory ShopType.fromJson(Map<String, dynamic> json) => ShopType(
    id: json["id"],
    name: json["name"],
    description: json["description"],
  );

  Map<String, dynamic> toJson() => {
    "id": id,
    "name": name,
    "description": description,
  };
}

这是 json 将来自 API 的响应

{
    "success": true,
    "data": {
        "shops": [
            {
                "id": 1,
                "name": "shop name a",
                "website": "http://www.test.com/",
                "email": "test@test.com",
                "description": "this is a dummy data",
                "shop_types": [
                    {
                        "id": 1,
                                "name": "Type A",
                                "description": "Type A",
                        "pivot": {
                            "shop_id": 1,
                            "shop_type_id": 1
                        }
                    }
                ]
            },
            {
                "id": 2,
                "name": "shop name b",
                "website": "http://www.test.com/",
                "email": "test@test.com",
                "description": "this is a dummy data",
                "shop_types": [
                    {
                        "id": 2,
                        "name": "Store",
                        "description": "Store",
                        "pivot": {
                            "shop_id": 2,
                            "shop_type_id": 2
                        }
                    }
                ]
            },
            {
                "id": 3,
                "name": "shop name c",
                "website": "http://www.test.com/",
                "email": "test@test.com",
                "description": "this is a dummy data",
                "shop_types": [
                    {
                        "id": 1,
                                "name": "Type A",
                                "description": "Type A",
                        "pivot": {
                            "shop_id": 3,
                            "shop_type_id": 1
                        }
                    },
                    {
                        "id": 2,
                                "name": "Type B",
                                "description": "Type B",
                        "pivot": {
                            "shop_id": 3,
                            "shop_type_id": 2
                        }
                    }
                ]
            }
        ],
        "shopTypes": [
            {
                "id": 1,
                "name": "Type A",
                "description": "Type A",
            },
            {
                "id": 2,
                "name": "Type B",
                "description": "Type B",
            }
        ]
    }
}

标签: flutterdart

解决方案


使用Dio从网络中获取数据

fetchShopsAndSaveLocal() async{
    var dio = Dio();
    dio.options.connectTimeout = 4500 * 10; //45s
    dio.options.receiveTimeout = 4500 * 10;

    try {
      var param = {
        "useremail": "dpk.7@gmail.com"
        , "password": "123456"};
      var response = await dio.post(
          "https://api.test/getstores", data: FormData.fromMap(param));
      var json = jsonDecode(response.data);
      ModelStore store = ModelStore.fromJson(json);
      List<Shops> shops = store.shops;


      for (var shop in shops) {
        saveEverySingleShopInDatabase(shop);
      }
    } catch (e) {}
  }

之后使用SQFlite

将店铺数据保存到数据库中,将数据保存到本地数据库后,每次从数据库中获取数据到listview。


推荐阅读