剩下的问题api flutter by package http

problem in the rest api flutter by package http

我想使用包 http 但我们没有得到模型的输出。我通过打印对每个步骤进行了编号,我意识到步骤编号 1-1-1 没有更进一步,并且 Chrome 浏览器给出了图像中的错误。

构建错误 Google Chrome

服务APIclass

    import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:test1/ModelTest.dart';

class ServiseApi {
  final httpcline = http.Client();
  Future<ModelUser> Login_Servise_Api() async {
    final keymodel = {'key': 'login', 'codeMEli': '1234567890', 'pass': '12345678'};

    final url = Uri.http('localhost', 'users.php', keymodel);
    print(url);
    final response = await http.get(url);

    print("ssssss" + response.toString());

    if (response.statusCode == 200) {
      final responcJson = jsonDecode(response.body);
      print('number1');
      print(responcJson.toString());
      print('number1-1');

     final  modelUser =await  ModelUser.fromJson(responcJson);
      print('number2');
      print(modelUser.toString());
      print('number3');
      return modelUser;
    } else {
      return ModelUser(
          id: 'e',
          codeOffice: '0',
          codemeli: '0',
          password: '0',
          fname: '0',
          lname: '0',
          expdate: '0',
          tel: '0',
          mobile: '0',
          city: '0',
          address: '0',
          date: '0',
          pish: '0',
          edare: '0',
          flag: '0',
          type: '0',
          img: '0');
    }
  }
}

型号class(数据class)

import 'dart:convert';

class ModelUser {
  final String id;
  final String codeOffice;
  final String codemeli;
  final String password;
  final String fname;
  final String lname;
  final String expdate;
  final String tel;
  final String mobile;
  final String city;
  final String address;
  final String date;
  final String pish;
  final String edare;
  final String flag;
  final String type;
  final String img;
  ModelUser({
    required this.id,
    required this.codeOffice,
    required this.codemeli,
    required this.password,
    required this.fname,
    required this.lname,
    required this.expdate,
    required this.tel,
    required this.mobile,
    required this.city,
    required this.address,
    required this.date,
    required this.pish,
    required this.edare,
    required this.flag,
    required this.type,
    required this.img,
  });

  ModelUser copyWith({
    String? id,
    String? codeOffice,
    String? codemeli,
    String? password,
    String? fname,
    String? lname,
    String? expdate,
    String? tel,
    String? mobile,
    String? city,
    String? address,
    String? date,
    String? pish,
    String? edare,
    String? flag,
    String? type,
    String? img,
  }) {
    return ModelUser(
      id: id ?? this.id,
      codeOffice: codeOffice ?? this.codeOffice,
      codemeli: codemeli ?? this.codemeli,
      password: password ?? this.password,
      fname: fname ?? this.fname,
      lname: lname ?? this.lname,
      expdate: expdate ?? this.expdate,
      tel: tel ?? this.tel,
      mobile: mobile ?? this.mobile,
      city: city ?? this.city,
      address: address ?? this.address,
      date: date ?? this.date,
      pish: pish ?? this.pish,
      edare: edare ?? this.edare,
      flag: flag ?? this.flag,
      type: type ?? this.type,
      img: img ?? this.img,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'codeOffice': codeOffice,
      'codemeli': codemeli,
      'password': password,
      'fname': fname,
      'lname': lname,
      'expdate': expdate,
      'tel': tel,
      'mobile': mobile,
      'city': city,
      'address': address,
      'date': date,
      'pish': pish,
      'edare': edare,
      'flag': flag,
      'type': type,
      'img': img,
    };
  }

  factory ModelUser.fromMap(Map<String, dynamic> map) {
    return ModelUser(
      id: map['id'],
      codeOffice: map['codeOffice'],
      codemeli: map['codemeli'],
      password: map['password'],
      fname: map['fname'],
      lname: map['lname'],
      expdate: map['expdate'],
      tel: map['tel'],
      mobile: map['mobile'],
      city: map['city'],
      address: map['address'],
      date: map['date'],
      pish: map['pish'],
      edare: map['edare'],
      flag: map['flag'],
      type: map['type'],
      img: map['img'],
    );
  }

  String toJson() => json.encode(toMap());

  factory ModelUser.fromJson(Map<String, dynamic> map) {
    return ModelUser(
      id: map['id'],
      codeOffice: map['codeOffice'],
      codemeli: map['codemeli'],
      password: map['password'],
      fname: map['fname'],
      lname: map['lname'],
      expdate: map['expdate'],
      tel: map['tel'],
      mobile: map['mobile'],
      city: map['city'],
      address: map['address'],
      date: map['date'],
      pish: map['pish'],
      edare: map['edare'],
      flag: map['flag'],
      type: map['type'],
      img: map['img'],
    );
  }
  @override
  String toString() {
    return 'ModelUser(id: $id, codeOffice: $codeOffice, codemeli: $codemeli, password: $password, fname: $fname, lname: $lname, expdate: $expdate, tel: $tel, mobile: $mobile, city: $city, address: $address, date: $date, pish: $pish, edare: $edare, flag: $flag, type: $type, img: $img)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is ModelUser &&
        other.id == id &&
        other.codeOffice == codeOffice &&
        other.codemeli == codemeli &&
        other.password == password &&
        other.fname == fname &&
        other.lname == lname &&
        other.expdate == expdate &&
        other.tel == tel &&
        other.mobile == mobile &&
        other.city == city &&
        other.address == address &&
        other.date == date &&
        other.pish == pish &&
        other.edare == edare &&
        other.flag == flag &&
        other.type == type &&
        other.img == img;
  }

  @override
  int get hashCode {
    return id.hashCode ^
        codeOffice.hashCode ^
        codemeli.hashCode ^
        password.hashCode ^
        fname.hashCode ^
        lname.hashCode ^
        expdate.hashCode ^
        tel.hashCode ^
        mobile.hashCode ^
        city.hashCode ^
        address.hashCode ^
        date.hashCode ^
        pish.hashCode ^
        edare.hashCode ^
        flag.hashCode ^
        type.hashCode ^
        img.hashCode;
  }
}

主要class

main() {
  ServiseApi().Login_Servise_Api();
}

输出调试

Error: Expected a value of type 'Map<String, dynamic>', but got one of type 'List<dynamic>'
    at Object.throw_ [as throw] (http://localhost:7668/dart_sdk.js:5063:11)
    at Object.castError (http://localhost:7668/dart_sdk.js:5022:15)
    at Object.cast [as as] (http://localhost:7668/dart_sdk.js:5345:17)
    at Function.as_C [as as] (http://localhost:7668/dart_sdk.js:4968:19)
    at Login_Servise_Api (http://localhost:7668/packages/test1/Servis.dart.lib.js:43:87)
    at Login_Servise_Api.next (<anonymous>)
    at http://localhost:7668/dart_sdk.js:37788:33
    at _RootZone.runUnary (http://localhost:7668/dart_sdk.js:37659:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:7668/dart_sdk.js:32861:29)
    at handleValueCallback (http://localhost:7668/dart_sdk.js:33413:49)
    at Function._propagateToListeners (http://localhost:7668/dart_sdk.js:33451:17)
    at _Future.new.[_completeWithValue] (http://localhost:7668/dart_sdk.js:33299:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:7668/dart_sdk.js:33320:35)
    at Object._microtaskLoop (http://localhost:7668/dart_sdk.js:37926:13)
    at _startMicrotaskLoop (http://localhost:7668/dart_sdk.js:37932:13)
    at http://localhost:7668/dart_sdk.js:33667:9
Restarted application in ۱۹۱ms.
http://localhost/users.php?key=login&codeMEli=1234567890&pass=12345678
ssssssInstance of 'Response'
number1
[{id: 61, code_office: 72371170, codemeli: 1234567890, password: 12345678, fname: مریم, lname: کریمی, expdate: 1398/11/02, tel: 33679501, mobile: 09177687005, city: بندرعباس, address: گلشهر شمالی کوی نواب نواب 12, date: ۱۳۹۶/۱۰/۱۴, pish: 1, edare: 0, flag: 1, type: 0, img: no_profile_img.jpeg}]
number1-1

因为你收到 List<Something> 并且你想要 Pars Map<String,SomeThing>

例如您收到

[
{something}
{something}
]

但是你的模型想要解析

{something}

独奏

将此代码添加到您的模型中

  static List<Category> fromList(Iterable l) {
    List<Category> data =
        List<Category>.from(l.map((model) => Category.fromJson(model)));
    return data;
  }

并如下使用

  var data = Category.fromList(json.decode(response.body));
  return data;