使用 Getx 将 API 连接到 Flutter 中的 UI

问题描述 投票:0回答:1

我正在尝试将 API 连接到 UI。我创建了 Json 类、产品控制器、产品模型和 API 服务。但是当运行这段代码时,我得到了一个空白页。我找不到数据列表。

有人可以帮忙解决这个问题吗?预先感谢!

产品列表查看代码:

class ProductListView extends StatelessWidget {
  final ProductController productController = Get.put(ProductController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(AppString.productList),
      ),
      body: Obx(() {
        if (productController.isLoading.value)
          return Center(
            child: CircularProgressIndicator(),
          );
        else
          return ListView.builder(
            itemCount: productController.productList.length,
            itemBuilder: (context, index) {
              return Column(
                children: [
                  Row(
                    children: [
                      Container(
                        width: 150,
                        height: 100,
                        margin: EdgeInsets.fromLTRB(16, 8, 8, 8),
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(8),
                          child: Image.network(
                              productController.productList[index].imageLink,
                              width: 150,
                              height: 100,
                              fit: BoxFit.fill,
                              color: AppColor.purplecolor,
                              colorBlendMode: BlendMode.color),
                        ),
                      ),
                      Flexible(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              productController.productList[index].title,
                              style: TextStyle(fontSize: 18),
                            ),
                            Text(
                              productController
                                  .productList.value[index].description,
                              style:
                                  TextStyle(fontSize: 18, color: AppColor.grey),
                            ),
                            Text(
                              productController.productList[index].brand,
                              style:
                                  TextStyle(fontSize: 18, color: AppColor.grey),
                            ),
                            Text(
                              productController.productList[index].price,
                              style:
                                  TextStyle(fontSize: 18, color: AppColor.grey),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                  Container(
                    color: AppColor.grey200,
                    height: 2,
                  ),
                ],
              );
            },
          );
      }),
    );
  }
}

产品型号代码:

import 'dart:convert';

List<ProductModel> productModelFromJson(String str) => List<ProductModel>.from(
    json.decode(str).map((x) => ProductModel.fromJson(x)));

String productModelToJson(List<ProductModel> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class ProductModel {
  int? id;
  String? title;
  String? description;
  int? price;
  double? discountPercentage;
  double? rating;
  int? stock;
  String? brand;
  String? category;
  String? thumbnail;
  List<String>? images;

  ProductModel(
      {this.id,
      this.title,
      this.description,
      this.price,
      this.discountPercentage,
      this.rating,
      this.stock,
      this.brand,
      this.category,
      this.thumbnail,
      this.images});

  ProductModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    title = json['title'];
    description = json['description'];
    price = json['price'];
    discountPercentage = json['discountPercentage'];
    rating = json['rating'];
    stock = json['stock'];
    brand = json['brand'];
    category = json['category'];
    thumbnail = json['thumbnail'];
    images = json['images'].cast<String>();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['title'] = this.title;
    data['description'] = this.description;
    data['price'] = this.price;
    data['discountPercentage'] = this.discountPercentage;
    data['rating'] = this.rating;
    data['stock'] = this.stock;
    data['brand'] = this.brand;
    data['category'] = this.category;
    data['thumbnail'] = this.thumbnail;
    data['images'] = this.images;
    return data;
  }
}

产品控制器中的产品模型类声明:

RxList productList = <ProductModel>[].obs;
flutter dart flutter-getx
1个回答
0
投票

在控制器内部,在onInit()方法中调用api

© www.soinside.com 2019 - 2024. All rights reserved.