Flutter GetX -将API连接到UI

siv3szwd  于 2023-03-03  发布在  Flutter
关注(0)|答案(1)|浏览(207)

我尝试使用GetX在Flutter中集成API和UI。但是当从JSON文件中检索产品时,我得到了以下错误。

错误:参数“id,title,description,price,discount,rating,stock,brand,category,thumbnail,images”的值不能为“null”,因为其类型不同,但隐式默认值为“null”。

所以我使用了'null safety'方法。然后当我调用那个类时,我得到了下面的错误。
var productList = ProductModel().obs;

错误:命名参数“id,name,description....”是必需的,但没有对应的参数。

Json代码:

ProductModel productModelFromJson(String str) =>
    ProductModel.fromJson(json.decode(str));

String productModelToJson(ProductModel data) => json.encode(data.toJson());

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

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

  factory ProductModel.fromJson(Map<String, dynamic> json) => ProductModel(
        id: json["id"],
        title: json["title"],
        description: json["description"],
        price: json["price"],
        discountPercentage: json["discountPercentage"]?.toDouble(),
        rating: json["rating"]?.toDouble(),
        stock: json["stock"],
        brand: json["brand"],
        category: json["category"],
        thumbnail: json["thumbnail"],
        images: List<String>.from(json["images"].map((x) => x)),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "title": title,
        "description": description,
        "price": price,
        "discountPercentage": discountPercentage,
        "rating": rating,
        "stock": stock,
        "brand": brand,
        "category": category,
        "thumbnail": thumbnail,
        "images": List<dynamic>.from(images.map((x) => x)),
      };
}

错误代码:

import 'package:get/state_manager.dart';
import 'package:practice/apimodule/api_service.dart';
import 'package:practice/productmodule/models/product_model.dart';
import 'package:get/get_rx/get_rx.dart';

class ProductController extends GetxController {
  var isLoading = true.obs;
  var productList = ProductModel().obs;
}
t9aqgxwy

t9aqgxwy1#

这都是关于*null-safety*的。如果你正在使用 * null-safety * 特性,那么你必须根据你当前的Pojo类通过构造函数初始化变量的值。你可以在你当前的构造函数中使用required或者使用一个未命名的构造函数。或者用?符号使你的字段nullable
更具体地说:
方式1(设为可空):

ProductModel productFromJson(String str) => ProductModel.fromJson(json.decode(str));
String productToJson(ProductModel data) => json.encode(data.toJson());

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

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


factory ProductModel.fromJson(Map<String, dynamic> json) => ProductModel(
    id: json["id"],
    title: json["title"],
    description: json["description"],
    price: json["price"],
    discountPercentage: json["discountPercentage"].toDouble(),
    rating: json["rating"].toDouble(),
    stock: json["stock"],
    brand: json["brand"],
    category: json["category"],
    thumbnail: json["thumbnail"],
    "images": List<dynamic>.from((images ?? []).map((x) => x) ),
  );


Map<String, dynamic> toJson() => {
    "id": id,
    "title": title,
    "description": description,
    "price": price,
    "discountPercentage": discountPercentage,
    "rating": rating,
    "stock": stock,
    "brand": brand,
    "category": category,
    "thumbnail": thumbnail,
    "images": List<dynamic>.from(images.map((x) => x)),
  };

方式2(制造要求):

ProductModel productFromJson(String str) => ProductModel.fromJson(json.decode(str));
String productToJson(ProductModel data) => json.encode(data.toJson());

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

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


factory ProductModel.fromJson(Map<String, dynamic> json) => ProductModel(
    id: json["id"],
    title: json["title"],
    description: json["description"],
    price: json["price"],
    discountPercentage: json["discountPercentage"].toDouble(),
    rating: json["rating"].toDouble(),
    stock: json["stock"],
    brand: json["brand"],
    category: json["category"],
    thumbnail: json["thumbnail"],
    images: List<String>.from(json["images"].map((x) => x)),
  );


Map<String, dynamic> toJson() => {
    "id": id,
    "title": title,
    "description": description,
    "price": price,
    "discountPercentage": discountPercentage,
    "rating": rating,
    "stock": stock,
    "brand": brand,
    "category": category,
    "thumbnail": thumbnail,
    "images": List<dynamic>.from(images.map((x) => x)),
  };

相关问题