import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  Future<List<Products>> getApiData() async {
    var url = "https://gorest.co.in/public-api/products";
    var response = await http.get(url);
    var jsonString = response.body;
    List<Products> products = productsFromJson(jsonString);

    print(jsonString);
    return products;   

  }

why it doesn't work, [A value of type 'Products' can't be assigned to a variable of type 'List'. Try changing the type of the variable, or casting the right-hand type to 'List'.;][1]

  [1]: https://i.stack.imgur.com/MQCEP.png

json to dart file
A value of type 'Products' can't be assigned to a variable of type 'List'. Try changing the type of the variable,dart(invalid_assignment)

Products productsFromJson(String str) => 
Products.fromJson(json.decode(str));
String productsToJson(Products data) => json.encode(data.toJson());
class Products {
  Products({
    this.code,
    this.meta,
    this.data,
  });
  int code;
  Meta meta;
  List<Datum> data;

  factory Products.fromJson(Map<String, dynamic> json) => Products(
        code: json["code"],
        meta: Meta.fromJson(json["meta"]),
        data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "code": code,
        "meta": meta.toJson(),
        "data": List<dynamic>.from(data.map((x) => x.toJson())),
      };
}
class Datum {
  Datum({
    this.id,
    this.name,
    this.description,
    this.image,
    this.price,
    this.discountAmount,
    this.status,
    this.categories,
  });
  int id;
  String name;
  String description;
  String image;
  String price;
  String discountAmount;
  bool status;
  List<Category> categories;


Solution 1: Mimu Saha Tishan

You created a Modle class for Products, so productsFromJson(jsonString) return a Products response. but you try to put the API response to List variable, and then it causes an error.

change your API call by below code it will work fine.

Future<Products> getApiData() async {
    var url = "https://gorest.co.in/public-api/products";
    var response = await http.get(url);
    var jsonString = response.body;
    Products products = productsFromJson(jsonString);

    print(jsonString);
    return products;   

}


Solution 2: Alperen Ekin

var url = "https://gorest.co.in/public-api/products&quot;;
var response = await http.get(url);
var jsonString = jsonDecode(response.body);
List<Products> products = jsonString.map((jsonMap) => Products.fromJson(jsonMap)).toList();

If you need to return all products as list, you can use like this.