I’m working on a Flutter application where I’m trying to send a POST request to a Node.js server to add a product. However, I’m encountering an issue where the server responds with a “Product is not a constructor” error. Here’s the setup:
admin_services.dart
import 'dart:convert';
import 'dart:io';
import 'package:cloudinary_public/cloudinary_public.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:provider/provider.dart';
import 'package:swadesh/constants/errors.dart';
import 'package:swadesh/constants/global_variables.dart';
import 'package:swadesh/constants/utils.dart';
import 'package:swadesh/models/product.dart';
import 'package:swadesh/providers/user_provider.dart';
class AdminServices {
void sellProduct({
required BuildContext context,
required String name,
required String description,
required double price,
required double quantity,
required String category,
required List<File> images,
}) async {
final userProvider = Provider.of<UserProvider>(context, listen: false);
try {
final cloudinary = CloudinaryPublic('dypdblusf', 'pmfi2yqj');
List<String> imageUrls = [];
// Upload images to Cloudinary and collect URLs
for (int i = 0; i < images.length; i++) {
CloudinaryResponse res = await cloudinary.uploadFile(
CloudinaryFile.fromFile(images[i].path, folder: name),
);
imageUrls.add(res.secureUrl);
}
// Create product object
Product product = Product(
name: name,
description: description,
quantity: quantity,
images: imageUrls,
category: category,
price: price,
);
// Convert product object to JSON
String jsonBody = json.encode(product.toMap());
// Send HTTP POST request to server
http.Response res = await http.post(
Uri.parse('$uri/admin/add-product'),
headers: {
'Content-Type': 'application/json; charset=UTF-8',
'x-auth-token': userProvider.user.token,
},
body: jsonBody,
);
// Handle the response
httpErrorHandle(
response: res,
context: context,
onSuccess: () {
showSnackBar(context, 'Product Added Successfully!');
Navigator.pop(context);
},
);
} catch (e) {
showSnackBar(context, e.toString());
}
}
}
product.dart
import 'dart:convert';
class Product {
final String name;
final String description;
final double quantity;
final List<String> images;
final String category;
final double price;
Product({
required this.name,
required this.description,
required this.quantity,
required this.images,
required this.category,
required this.price,
});
Map<String, dynamic> toMap() {
return <String, dynamic>{
'name': name,
'description': description,
'quantity': quantity,
'images': images,
'category': category,
'price': price,
};
}
factory Product.fromMap(Map<String, dynamic> map) {
return Product(
name: map['name'],
description: map['description'],
quantity: map['quantity'].toDouble(),
images: List<String>.from(map['images']),
category: map['category'],
price: map['price'].toDouble(),
);
}
String toJson() => json.encode(toMap());
factory Product.fromJson(String source) =>
Product.fromMap(json.decode(source));
}
When I run this code, the server responds with:
{
"error": "Product is not a constructor"
}
I’ve checked that the Product model is correctly defined and imported in my server-side code.
The JSON payload sent from Dart looks like this: {“name”:”Product Name”,”description”:”Product Description”,”quantity”:10,”images”:[“url1″,”url2″,”url3″],”category”:”Category Name”,”price”:99.99}.
The Product class in Dart is structured to match the expected structure on the Node.js server.
What could be causing this issue and how can I resolve it? Any help would be appreciated. Thank you!
If you are able to view the expected payload in the request then client side code is correct. I think this is a server side issue and you or your team is sending the runtime exception as the response.
Looking at the response “Product is not a constructor”, I think it is a common js error. Kindly look into your server side code or ask your backend team to verify.
Similar question(js) about “* is not a constructor” is answered here.
Parag Dalvi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.