flutter 无此类方法错误:类“int”没有示例方法“[]”

z5btuh9x  于 2022-11-30  发布在  Flutter
关注(0)|答案(1)|浏览(153)

我尝试使用laravel api获取mysql数据并传递给列表,但它返回“NoSuchMethodError:类“int”没有示例方法“[]”。”

错误

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

这是错误

提供程序(ForEach所在位置)

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

我试着得到所有的“公告”数据并传递给一个列表

型号

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控制器

这是Dio.get调用的函数

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

==========

响应。数据示例:

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

应该是这样的

response variable debug

xuo3flqw

xuo3flqw1#

您的问题在于此逻辑:

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

您的API只返回单个Record,而不是数组或多个记录,因此根本不需要forEach()

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

如果出于某种原因您希望保留.forEach(),则需要更改API以返回数组:

$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]
]);

您可以使用where('id', $idFrAnunFr)->get()使$anunfr成为Collection(数组),也可以使用::find($idFrAnunFr)并通过'anuncios' => [$anunfr]将此单个记录作为数组返回

相关问题