Flutter未处理异常:类型“String”不是“index”的类型“int”的子类型(从API获取数据时)

iezvtpos  于 2023-06-07  发布在  Flutter
关注(0)|答案(1)|浏览(233)

我试图从API中获取数据并显示到下拉列表中,但它引发了一个错误**“Unhandled Exception:type 'String' is not a subtype of type 'int' of 'index'"**当我打印json body时,它显示了完美的输出。
以下是我的代码:

MyModelClass:

import 'dart:convert';
/// status : true
/// result : "found"

DropDownModel dropDownModelFromJson(String str) => DropDownModel.fromJson(json.decode(str));
String dropDownModelToJson(DropDownModel data) => json.encode(data.toJson());
class DropDownModel {
  DropDownModel({
      bool? status, 
      String? result, 
      List<Data>? data,}){
    _status = status;
    _result = result;
    _data = data;
}

  DropDownModel.fromJson(dynamic json) {
    _status = json['status'];
    _result = json['result'];
    if (json['data'] != null) {
      _data = [];
      json['data'].forEach((v) {
        _data?.add(Data.fromJson(v));
      });
    }
  }
  bool? _status;
  String? _result;
  List<Data>? _data;
DropDownModel copyWith({  bool? status,
  String? result,
  List<Data>? data,
}) => DropDownModel(  status: status ?? _status,
  result: result ?? _result,
  data: data ?? _data,
);
  bool? get status => _status;
  String? get result => _result;
  List<Data>? get data => _data;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['status'] = _status;
    map['result'] = _result;
    if (_data != null) {
      map['data'] = _data?.map((v) => v.toJson()).toList();
    }
    return map;
  }

}

/// id : "103203100154936642435"
/// name : "King Ali"
/// email : "ka8769900@gmail.com"
/// isAdmin : "no"

Data dataFromJson(String str) => Data.fromJson(json.decode(str));
String dataToJson(Data data) => json.encode(data.toJson());
class Data {
  Data({
      String? id, 
      String? name, 
      String? email, 
      String? isAdmin,}){
    _id = id;
    _name = name;
    _email = email;
    _isAdmin = isAdmin;
}

  Data.fromJson(dynamic json) {
    _id = json['id'];
    _name = json['name'];
    _email = json['email'];
    _isAdmin = json['isAdmin'];
  }
  String? _id;
  String? _name;
  String? _email;
  String? _isAdmin;
Data copyWith({  String? id,
  String? name,
  String? email,
  String? isAdmin,
}) => Data(  id: id ?? _id,
  name: name ?? _name,
  email: email ?? _email,
  isAdmin: isAdmin ?? _isAdmin,
);
  String? get id => _id;
  String? get name => _name;
  String? get email => _email;
  String? get isAdmin => _isAdmin;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = _id;
    map['name'] = _name;
    map['email'] = _email;
    map['isAdmin'] = _isAdmin;
    return map;
  }

}

API取数

getData() async{
  var res = await http.get(Uri.parse(link));

  if(res.statusCode == 200){

    //print(res.body);
    DropDownModel data = DropDownModel.fromJson(res.body);

    //print(data);
    return data;
  }else{
    print("Somet thing went wrong");
  }
}

将数据列表插入数组。

List<Data> customerList = [];
  String? selectedCustomer;

customerDropDwon() async{
    DropDownModel data = await getData();
    setState(() {
      customerList = data!.data!;
    });
  }

 @override
  void initState() {
    // TODO: implement initState
    super.initState();
    customerDropDwon();
  }

DropdownButton(
                    hint: Text("customer"),
                      items: customerList.map((e){
                        return DropdownMenuItem(child: Text(e.name.toString()),
                        value: e.id.toString(),);
                      }).toList(), onChanged: (newVal){
                        setState(() {
                          selectedCustomer =newVal;
                        });

错误完美定位:

下拉模式:

DropDownModel.fromJson(dynamic json) {
    _status = json['status'];
    _result = json['result'];
    if (json['data'] != null) {
      _data = [];
      json['data'].forEach((v) {
        _data?.add(Data.fromJson(v));
      });
    }
  }

getData方法:

DropDownModel data = DropDownModel.fromJson(res.body);

添加列表:

customerDropDwon() async{
    DropDownModel data = await getData();
    setState(() {
      customerList = data!.data!;
    });
  }

解决办法是什么?

wa7juj8i

wa7juj8i1#

您是否尝试在模型中更改“id”的数据类型?String转int(仅主键)

Data({
  int? id, 
  String? name, 
  String? email, 
  String? isAdmin,}){
_id = id;
_name = name;
_email = email;
_isAdmin = isAdmin;

}

相关问题