Home > Mobile >  NoSuchMethodError: Class 'int' has no instance method '[]'
NoSuchMethodError: Class 'int' has no instance method '[]'

Time:11-29

I'm trying to get mysql data using laravel api and passing to a list, but it is returning "NoSuchMethodError: Class 'int' has no instance method '[]'."

=========

Error

I/flutter ( 8481): NoSuchMethodError: Class 'int' has no instance method '[]'.
I/flutter ( 8481): Receiver: 1
I/flutter ( 8481): Tried calling: []("idAnunFr")

This is the error

Provider (where the ForEach is)

import 'package:bicos_app/model/anuncio_Freelancer.dart';
import 'package:bicos_app/model/freelancer.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../utils/freelancer_preferences.dart';
import '../utils/user_preferences.dart';

class AnunFreelancerProvider with ChangeNotifier {
  late Freelancer freelancer;

  List<AnuncioFreelancer> _anunciosMyFreelancer = [];
  List<AnuncioFreelancer> getAnunciosMyFreelancer() => _anunciosMyFreelancer;

  Future<dynamic> loadAnunMyFreelancer(int id) async {
    try {
      _anunciosMyFreelancer.clear();
      var response = await Dio()
          .get('http://10.0.2.2:8000/api/getAnunFreelancerByFreelancer/$id');
      if (response.data['status'] == '200') {
        response.data['anuncios'].forEach(
          (k, e) {
            AnuncioFreelancer anuncio = AnuncioFreelancer(
              idAnunFr: e['idAnunFr'],
              TituloAnunFr: e['TituloAnunFr'],
              DescAnunFr: e['DescAnunFr'],
              PrecoAnunFr: e['PrecoAnunFr'],
              ImgAnunFr: e['ImgAnunFr'],
              StatusAnunFr: e['StatusAnunFr'],
              DataAnunFr: e['DataAnunFr'],
              idFrAnunFr: e['idFrAnunFr'],
              idTipoServAnunFr: e['idTipoServAnunFr'],
            );
            if (anuncio.StatusAnunFr == '1') {
              if (_anunciosMyFreelancer
                  .any((element) => element.idAnunFr == anuncio.idAnunFr)) {
                print('_');
              } else {
                _anunciosMyFreelancer.add(anuncio);
              }
            }
          },
        );
      } else {
        print(response.data['message'].toString());
      }
      notifyListeners();
    } catch (e) {
      print(e);
    }
  }
}

I tried to get all the 'anuncios' data and pass to a list

=========

Model

class AnuncioFreelancer {
  final int idAnunFr;
  final String TituloAnunFr;
  final String DescAnunFr;
  final double PrecoAnunFr;
  final String ImgAnunFr;
  final String StatusAnunFr;
  final String DataAnunFr;
  final int idFrAnunFr;
  final int idTipoServAnunFr;

  const AnuncioFreelancer({
    required this.idAnunFr,
    required this.TituloAnunFr,
    required this.DescAnunFr,
    required this.PrecoAnunFr,
    required this.ImgAnunFr,
    required this.StatusAnunFr,
    required this.DataAnunFr,
    required this.idFrAnunFr,
    required this.idTipoServAnunFr,
  });
}

========

Laravel Api Controller

this is the function that Dio.get is calling

public function getAnunFreelancerByFreelancer($idFrAnunFr)
    {
        if(TblAnunFreelancer::where('idFrAnunFr', $idFrAnunFr)->exists())
        {
            $anunfr = TblAnunFreelancer::find($idFrAnunFr);

            return response()->json([
                'status'=>'200',
                'anuncios'=>$anunfr,
            ]);
        } else {
            return response()->json([
                'status'=>'400',
                'message'=>'Você não possui anúncios',
            ]);
        }
    }

==========

response.data sample:

idAnunFr: 1,
TituloAnunFr: 'Title',
DescAnunFr: 'Description',
PrecoAnunFr: 200.00,
ImgAnunFr: 'assets/images/testeImagemAnun.png',
StatusAnunFr: '1',
DataAnunFr: '2022-11-27',
idFrAnunFr: 1,
idTipoServAnunFr: 1,

it was supposed to get something like this

================

response variable debug

CodePudding user response:

Your issue is in this logic:

response.data['anuncios'].forEach( ... )

Your API is only returning a single Record, not an array or multiple records, so there is no need for the forEach() at all:

if (response.data['status'] == '200') {
  AnuncioFreelancer anuncio = AnuncioFreelancer(
    idAnunFr: response.data['anuncios']['idAnunFr'],
    TituloAnunFr: response.data['anuncios']['TituloAnunFr'],
    DescAnunFr: response.data['anuncios']['DescAnunFr'],
    PrecoAnunFr: response.data['anuncios']['PrecoAnunFr'],
    ImgAnunFr: response.data['anuncios']['ImgAnunFr'],
    StatusAnunFr: response.data['anuncios']['StatusAnunFr'],
    DataAnunFr: response.data['anuncios']['DataAnunFr'],
    idFrAnunFr: response.data['anuncios']['idFrAnunFr'],
    idTipoServAnunFr: response.data['anuncios']['idTipoServAnunFr'],
  );
}

If, for some reason you want to keep the .forEach(), then you'd need to change your API to return an array:

$anunfr = TblAnunFreelancer::where('id', $idFrAnunFr)->get();

return response()->json([
  'status' => '200',
  'anuncios' => $anunfr
]);

// OR

$anunfr = TblAnunFreelancer::find($idFrAnunFr);

return response()->json([
  'status' => '200',
  'anuncios' => [$anunfr]
]);

You can either use where('id', $idFrAnunFr)->get() to make $anunfr a Collection (array), or use ::find($idFrAnunFr) and return this single record as an array via 'anuncios' => [$anunfr]

  • Related