我尝试使用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,
1条答案
按热度按时间xuo3flqw1#
您的问题在于此逻辑:
您的API只返回单个Record,而不是数组或多个记录,因此根本不需要
forEach()
:如果出于某种原因您希望保留
.forEach()
,则需要更改API以返回数组:您可以使用
where('id', $idFrAnunFr)->get()
使$anunfr
成为Collection(数组),也可以使用::find($idFrAnunFr)
并通过'anuncios' => [$anunfr]
将此单个记录作为数组返回