Priya
Priya

Reputation: 95

Flutter GetX - Connect API to UI

I am trying to integrate API with UI in Flutter using GetX. But while retrieving the product from the JSON file I got the below errors.

Error: The parameter 'id, title, description, price, discount, rating, stock, brand, category, thumbnail, images' can't have a value of 'null' because of its type, but the implicit default value is 'null'.

So I used the 'null safety' method. then I get the below error when I call that class.

var productList = ProductModel().obs;

Error: The named parameter 'id, name, description....' is required, but there's no corresponding argument.

Json code:

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)),
      };
}

Error code:

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;
}

Upvotes: -1

Views: 190

Answers (1)

Rafat Meraz
Rafat Meraz

Reputation: 1

It's all about null-safety. If you are using null-safety feature, then you must initialise the value of variable via constructor as per your current Pojo class. You can use required in your current constructor or use a unnamed constructor. Or make your fields nullable with ? sign.

More specific:

WAY 1 (Make nullable):

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)),
  };

WAY 2 (Make required):

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)),
  };

Upvotes: 0

Related Questions