Home > Software design >  NoSuchMethodError: Class '_InternalLinkedHashMap<String, dynamic>' has no instance m
NoSuchMethodError: Class '_InternalLinkedHashMap<String, dynamic>' has no instance m

Time:08-26

I'm trying to add a product to the postgres database using a laravel API, however I'm not managing to handle its return and even changing the return form it always shows another error could someone help me, please this is my first project using flutter and dart because of this I still don't have much knowledge.

These are my codes:

1º RequestsRepository

import 'package:project001_app_waiter/constants/endpoints.dart';
import 'package:project001_app_waiter/models/products_table_model.dart';
import 'package:project001_app_waiter/pages/common_widgets/result/data_result.dart';
import 'package:project001_app_waiter/pages/menu/result/menu_product_result.dart';
import 'package:project001_app_waiter/services/http_manager.dart';

class RequestsRepository {
  final HttpManager _httpManager = HttpManager();

  Future<MenuProductResult<Map<String, dynamic>>> addProductTable({
    required int productsId,
    required int tableId,
    required String quantity,
    required String status,
    String? complement,
    required String productName,
    required String imgUrl,
    required String price,
    required String unit,
    required String description,
  }) async {
    final result = await _httpManager.restRequest(
        url: Endpoints.addProductsTable,
        method: HttpMethods.post,
        body: {
          'products_id': productsId,
          'table_id': tableId,
          'quantity': quantity,
          'status': status,
          'complement': complement,
          productName: productName,
          imgUrl: imgUrl,
          price: price,
          unit: unit,
          description: description,
        });

    if (result != null) {
      return MenuProductResult<Map<String, dynamic>>.success(result);
    } else {
      return MenuProductResult.error(
        'Não foi possível adicionar este pedido',
      );
    }
  }
}

2º RequestsController

import 'package:get/get.dart';
import 'package:project001_app_waiter/pages/common_widgets/result/data_result.dart';
import 'package:project001_app_waiter/pages/menu/result/menu_product_result.dart';
import 'package:project001_app_waiter/pages/requests/repository/requests_repository.dart';
import 'package:project001_app_waiter/services/utils_services.dart';
import '../../../models/products_table_model.dart';

class RequestsController extends GetxController {
  final requestsRepository = RequestsRepository();
  final utilsServices = UtilsServices();

  bool isLoading = false;
  List<ProductsTableModel> allProductsTable = [];

  void setLoading(bool value) {
    isLoading = value;
    update();
  }

  @override
  void onInit() {
    super.onInit();

    getAllProductsTable();
  }

  Future<void> addProductsTable({
    required int product,
    required int table,
    required String status,
    String quantity = '1',
    String complement = '',
    required String productName,
    required String imgUrl,
    required String price,
    required String unit,
    required String description,
  }) async {
    final MenuProductResult result = await requestsRepository.addProductTable(
      productsId: product,
      tableId: table,
      quantity: quantity,
      status: status,
      complement: complement,
      productName: productName,
      imgUrl: imgUrl,
      price: price,
      unit: unit,
      description: description,
    );

    result.when(
      success: (allProductsTable) {
        allProductsTable.add(
          ProductsTableModel(
            productsId: product,
            tableId: table,
            quantity: quantity,
            status: status,
            complement: complement,
            productName: productName,
            imgUrl: imgUrl,
            price: price,
            unit: unit,
            description: description,
          ),
        );
      },
      error: (message) {
        utilsServices.showToast(
          message: message,
          isError: true,
        );
      },
    );

    update();
  }
}

Return from api

{
    "products_id": "2",
    "table_id": "6",
    "quantity": "5",
    "status": "3",
    "complement": "Um complemento para teste",
    "updated_at": "2022-08-23T18:54:17.000000Z",
    "created_at": "2022-08-23T18:54:17.000000Z",
    "id": 11
}

Model

import 'package:freezed_annotation/freezed_annotation.dart';

part 'products_table_model.g.dart';

@JsonSerializable()
class ProductsTableModel {
  int? id;

  @JsonKey(name: 'products_id')
  int productsId;

  @JsonKey(name: 'table_id')
  int tableId;
  String quantity;

  @JsonKey(name: 'created_at')
  DateTime? data;
  String? productName;
  String? imgUrl;
  String? price;
  String? unit;
  String? description;
  String complement;
  String status;

  ProductsTableModel({
    this.id,
    required this.productsId,
    required this.tableId,
    required this.quantity,
    this.data,
    this.productName,
    this.imgUrl,
    this.price,
    this.unit,
    this.description,
    this.complement = '',
    required this.status,
  });

  factory ProductsTableModel.fromJson(Map<String, dynamic> json) =>
      _$ProductsTableModelFromJson(json);

  Map<String, dynamic> toJson() => _$ProductsTableModelToJson(this);

  @override
  String toString() =>
      'ProductsTable(id: $id, productsId: $productsId, tableId: $tableId, quantity: $quantity, data: $data, productName: $productName, imgUrl: $imgUrl, price: $price, unit: $unit, description: $description, complement: $complement)';
}

CodePudding user response:

I think the problem is that the name of the your local varibale allProductsTable is the same as response coming back from the request which is a map and does not have add method. so in that scope you are using the map and not the list .

change it to some other name like allProductsTableResponse:

result.when(
  success: (allProductsTableResponse) {
    allProductsTable.add(
      ProductsTableModel(
        productsId: product,
        tableId: table,
        quantity: quantity,
        status: status,
        complement: complement,
        productName: productName,
        imgUrl: imgUrl,
        price: price,
        unit: unit,
        description: description,
      ),
    );
  },
  • Related