无法将类型“ X”的值分配给类型“ List <X>”的变量

时间:2020-05-10 11:59:30

标签: android json list flutter dart

我正在学习颤动,并尝试像在此article中一样解析JSON,但收到此错误。

lib / service / apiservice.dart:11:33:错误:“数据”类型的值不能 分配给“列表”类型的变量。 -“数据”来自“ package:gocorona / models / totals.dart”(“ lib / models / totals.dart”)。 -“列表”来自“ dart:core”。 最终列表数据= dataFromJson(response.body);

lib / service / apiservice.dart

import 'package:http/http.dart' as http;
import 'package:gocorona/models/totals.dart';

class ApiServices {

  static const String url = 'https://api.rootnet.in/covid19-in/stats/latest';
static Future<List<Data>> getDataFromAPI() async {
    try {
      final response = await http.get(url);
      if (200 == response.statusCode) {
        final List<Data> data = dataFromJson(response.body);
        return data;
      } else {
        return List<Data>();
      }
    } catch (e) {
      return List<Data>();
    }
  }
}

lib / models / totals.dart

import 'dart:convert';

Data dataFromJson(String str) => Data.fromJson(json.decode(str));

String dataToJson(Data data) => json.encode(data.toJson());

class Data {
    bool success;
    DataClass data;
    DateTime lastRefreshed;
    DateTime lastOriginUpdate;

    Data({
        this.success,
        this.data,
        this.lastRefreshed,
        this.lastOriginUpdate,
    });

    factory Data.fromJson(Map<String, dynamic> json) => Data(
        success: json["success"],
        data: DataClass.fromJson(json["data"]),
        lastRefreshed: DateTime.parse(json["lastRefreshed"]),
        lastOriginUpdate: DateTime.parse(json["lastOriginUpdate"]),
    );

    Map<String, dynamic> toJson() => {
        "success": success,
        "data": data.toJson(),
        "lastRefreshed": lastRefreshed.toIso8601String(),
        "lastOriginUpdate": lastOriginUpdate.toIso8601String(),
    };
}

class DataClass {
    Summary summary;
    List<UnofficialSummary> unofficialSummary;
    List<Regional> regional;

    DataClass({
        this.summary,
        this.unofficialSummary,
        this.regional,
    });

    factory DataClass.fromJson(Map<String, dynamic> json) => DataClass(
        summary: Summary.fromJson(json["summary"]),
        unofficialSummary: List<UnofficialSummary>.from(json["unofficial-summary"].map((x) => UnofficialSummary.fromJson(x))),
        regional: List<Regional>.from(json["regional"].map((x) => Regional.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "summary": summary.toJson(),
        "unofficial-summary": List<dynamic>.from(unofficialSummary.map((x) => x.toJson())),
        "regional": List<dynamic>.from(regional.map((x) => x.toJson())),
    };
}

class Regional {
    String loc;
    int confirmedCasesIndian;
    int discharged;
    int deaths;
    int confirmedCasesForeign;
    int totalConfirmed;

    Regional({
        this.loc,
        this.confirmedCasesIndian,
        this.discharged,
        this.deaths,
        this.confirmedCasesForeign,
        this.totalConfirmed,
    });

    factory Regional.fromJson(Map<String, dynamic> json) => Regional(
        loc: json["loc"],
        confirmedCasesIndian: json["confirmedCasesIndian"],
        discharged: json["discharged"],
        deaths: json["deaths"],
        confirmedCasesForeign: json["confirmedCasesForeign"],
        totalConfirmed: json["totalConfirmed"],
    );

    Map<String, dynamic> toJson() => {
        "loc": loc,
        "confirmedCasesIndian": confirmedCasesIndian,
        "discharged": discharged,
        "deaths": deaths,
        "confirmedCasesForeign": confirmedCasesForeign,
        "totalConfirmed": totalConfirmed,
    };
}

class Summary {
    int total;
    int confirmedCasesIndian;
    int confirmedCasesForeign;
    int discharged;
    int deaths;
    int confirmedButLocationUnidentified;

    Summary({
        this.total,
        this.confirmedCasesIndian,
        this.confirmedCasesForeign,
        this.discharged,
        this.deaths,
        this.confirmedButLocationUnidentified,
    });

    factory Summary.fromJson(Map<String, dynamic> json) => Summary(
        total: json["total"],
        confirmedCasesIndian: json["confirmedCasesIndian"],
        confirmedCasesForeign: json["confirmedCasesForeign"],
        discharged: json["discharged"],
        deaths: json["deaths"],
        confirmedButLocationUnidentified: json["confirmedButLocationUnidentified"],
    );

    Map<String, dynamic> toJson() => {
        "total": total,
        "confirmedCasesIndian": confirmedCasesIndian,
        "confirmedCasesForeign": confirmedCasesForeign,
        "discharged": discharged,
        "deaths": deaths,
        "confirmedButLocationUnidentified": confirmedButLocationUnidentified,
    };
}

class UnofficialSummary {
    String source;
    int total;
    int recovered;
    int deaths;
    int active;

    UnofficialSummary({
        this.source,
        this.total,
        this.recovered,
        this.deaths,
        this.active,
    });

    factory UnofficialSummary.fromJson(Map<String, dynamic> json) => UnofficialSummary(
        source: json["source"],
        total: json["total"],
        recovered: json["recovered"],
        deaths: json["deaths"],
        active: json["active"],
    );

    Map<String, dynamic> toJson() => {
        "source": source,
        "total": total,
        "recovered": recovered,
        "deaths": deaths,
        "active": active,
    };
}

lib / screens / states.dart

import 'package:flutter/material.dart';
import 'package:gocorona/models/totals.dart';
import 'package:gocorona/service/apiservice.dart';

class Statewise extends StatefulWidget {
  @override
  _StatewiseState createState() => _StatewiseState();
}

class _StatewiseState extends State<Statewise> {
  List<Data> _datafromApi;
  bool _isloading;

  @override
  void initState() {
    super.initState();

    _isloading = true;
    ApiServices.getDataFromAPI().then((data) {
      setState(() {
        _datafromApi = data;
        _isloading = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    if (_isloading) {
      return Container(
        child: Center(
          child: CircularProgressIndicator(),
        ),
      );
    } else {
      return Scaffold(
        body: ListView.builder(
            itemCount: _datafromApi == null ? 0 : _datafromApi.length,
            itemBuilder: (context, index) {
              Data datas = _datafromApi[index];
              print(datas?.toString() ?? "Empty");
              return ListTile(
                  title: Text(
                datas.data.regional[0].loc.toString(),
                style: TextStyle(color: Colors.black),
              ));
            }),
      );
    }
  }
}

编译器消息:

lib/service/apiservice.dart:11:33: Error: A value of type 'Data' can't be assigned to a variable of type 'List<Data>'.
 - 'Data' is from 'package:gocorona/models/totals.dart' ('lib/models/totals.dart').
 - 'List' is from 'dart:core'.
        final List<Data> data = dataFromJson(response.body);
                                ^

颤抖的医生

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, v1.17.0, on Microsoft Windows [Version 10.0.18362.778], locale en-IN)

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[√] Android Studio (version 3.6)
[√] VS Code (version 1.45.0)
[√] Connected device (1 available)

• No issues found!

3 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为您仅从构造函数中返回数据,并且试图将其接受为列表。

我做了一些更改,希望对您有所帮助。

class ApiServices {
  static const String url = 'https://api.rootnet.in/covid19-in/stats/latest';
  static Future<Data> getDataFromAPI() async {
    try {
      final response = await http.get(url);
      if (200 == response.statusCode) {
        print(response.toString());

        final data = dataFromJson(response.body);

        //  print(data);
        return data;
      } else {
        return Data();
      }
    } catch (e) {
      return Data();
    }
  }
}

Data dataFromJson(String str) => Data.fromJson(json.decode(str));

String dataToJson(Data data) => json.encode(data.toJson());

class Data {
  bool success;
  DataClass data;
  DateTime lastRefreshed;
  DateTime lastOriginUpdate;

  Data({
    this.success,
    this.data,
    this.lastRefreshed,
    this.lastOriginUpdate,
  });

  factory Data.fromJson(Map<String, dynamic> json) => Data(
        success: json["success"],
        data: DataClass.fromJson(json["data"]),
        lastRefreshed: DateTime.parse(json["lastRefreshed"]),
        lastOriginUpdate: DateTime.parse(json["lastOriginUpdate"]),
      );

  Map<String, dynamic> toJson() => {
        "success": success,
        "data": data.toJson(),
        "lastRefreshed": lastRefreshed.toIso8601String(),
        "lastOriginUpdate": lastOriginUpdate.toIso8601String(),
      };
}

class DataClass {
  Summary summary;
  List<UnofficialSummary> unofficialSummary;
  List<Regional> regional;

  DataClass({
    this.summary,
    this.unofficialSummary,
    this.regional,
  });

  factory DataClass.fromJson(Map<String, dynamic> json) => DataClass(
        summary: Summary.fromJson(json["summary"]),
        unofficialSummary: List<UnofficialSummary>.from(
            json["unofficial-summary"]
                .map((x) => UnofficialSummary.fromJson(x))),
        regional: List<Regional>.from(
            json["regional"].map((x) => Regional.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "summary": summary.toJson(),
        "unofficial-summary":
            List<dynamic>.from(unofficialSummary.map((x) => x.toJson())),
        "regional": List<dynamic>.from(regional.map((x) => x.toJson())),
      };
}

class Regional {
  String loc;
  int confirmedCasesIndian;
  int discharged;
  int deaths;
  int confirmedCasesForeign;
  int totalConfirmed;

  Regional({
    this.loc,
    this.confirmedCasesIndian,
    this.discharged,
    this.deaths,
    this.confirmedCasesForeign,
    this.totalConfirmed,
  });

  factory Regional.fromJson(Map<String, dynamic> json) => Regional(
        loc: json["loc"],
        confirmedCasesIndian: json["confirmedCasesIndian"],
        discharged: json["discharged"],
        deaths: json["deaths"],
        confirmedCasesForeign: json["confirmedCasesForeign"],
        totalConfirmed: json["totalConfirmed"],
      );

  Map<String, dynamic> toJson() => {
        "loc": loc,
        "confirmedCasesIndian": confirmedCasesIndian,
        "discharged": discharged,
        "deaths": deaths,
        "confirmedCasesForeign": confirmedCasesForeign,
        "totalConfirmed": totalConfirmed,
      };
}

class Summary {
  int total;
  int confirmedCasesIndian;
  int confirmedCasesForeign;
  int discharged;
  int deaths;
  int confirmedButLocationUnidentified;

  Summary({
    this.total,
    this.confirmedCasesIndian,
    this.confirmedCasesForeign,
    this.discharged,
    this.deaths,
    this.confirmedButLocationUnidentified,
  });

  factory Summary.fromJson(Map<String, dynamic> json) => Summary(
        total: json["total"],
        confirmedCasesIndian: json["confirmedCasesIndian"],
        confirmedCasesForeign: json["confirmedCasesForeign"],
        discharged: json["discharged"],
        deaths: json["deaths"],
        confirmedButLocationUnidentified:
            json["confirmedButLocationUnidentified"],
      );

  Map<String, dynamic> toJson() => {
        "total": total,
        "confirmedCasesIndian": confirmedCasesIndian,
        "confirmedCasesForeign": confirmedCasesForeign,
        "discharged": discharged,
        "deaths": deaths,
        "confirmedButLocationUnidentified": confirmedButLocationUnidentified,
      };
}

class UnofficialSummary {
  String source;
  int total;
  int recovered;
  int deaths;
  int active;

  UnofficialSummary({
    this.source,
    this.total,
    this.recovered,
    this.deaths,
    this.active,
  });

  factory UnofficialSummary.fromJson(Map<String, dynamic> json) =>
      UnofficialSummary(
        source: json["source"],
        total: json["total"],
        recovered: json["recovered"],
        deaths: json["deaths"],
        active: json["active"],
      );

  Map<String, dynamic> toJson() => {
        "source": source,
        "total": total,
        "recovered": recovered,
        "deaths": deaths,
        "active": active,
      };
}

class Statewise extends StatefulWidget {
  @override
  _StatewiseState createState() => _StatewiseState();
}

class _StatewiseState extends State<Statewise> {
  Data _datafromApi;
  bool _isloading;

  @override
  void initState() {
    super.initState();

    _isloading = true;
    ApiServices.getDataFromAPI().then((data) {
      setState(() {
        _datafromApi = data;
        _isloading = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    if (_isloading) {
      return Container(
        child: Center(
          child: CircularProgressIndicator(),
        ),
      );
    } else {
      return Scaffold(
        body: ListView.builder(
            itemCount:
                _datafromApi == null ? 0 : _datafromApi.data.regional.length,
            itemBuilder: (context, index) {
              return ListTile(
                  title: Text(
                _datafromApi.data.regional[index].loc.toString(),
                style: TextStyle(color: Colors.black),
              ));
            }),
      );
    }
  }
}

答案 1 :(得分:0)

你在做什么

List<Data> data = Data();
//trying to assign a Data to List of Data: List<Data>

//which is very similar to
List<String> aString = String();
//String should be treated as String but you are treating it as List<String>

这将起作用

Data data = Data()
//in your case
Data data = dataFromJson();

答案 2 :(得分:0)

最初,当您点击api时,您会收到一个完整的单个对象作为响应

var data = dataFromJson(response.body);

以上数据是单个对象而不是对象列表List 因此,一开始您可能会获得完整的对象。我根据您的书面例子在下面做了一个例子。

基于json数据的模型类

// To parse this JSON data, do
//
//     final data = dataFromJson(jsonString);

import 'dart:convert';

Data dataFromJson(String str) => Data.fromJson(json.decode(str));

String dataToJson(Data data) => json.encode(data.toJson());

class Data {
    bool success;
    DataClass data;
    DateTime lastRefreshed;
    DateTime lastOriginUpdate;

    Data({
        this.success,
        this.data,
        this.lastRefreshed,
        this.lastOriginUpdate,
    });

    factory Data.fromJson(Map<String, dynamic> json) => Data(
        success: json["success"],
        data: DataClass.fromJson(json["data"]),
        lastRefreshed: DateTime.parse(json["lastRefreshed"]),
        lastOriginUpdate: DateTime.parse(json["lastOriginUpdate"]),
    );

    Map<String, dynamic> toJson() => {
        "success": success,
        "data": data.toJson(),
        "lastRefreshed": lastRefreshed.toIso8601String(),
        "lastOriginUpdate": lastOriginUpdate.toIso8601String(),
    };
}

class DataClass {
    Summary summary;
    List<UnofficialSummary> unofficialSummary;
    List<Regional> regional;

    DataClass({
        this.summary,
        this.unofficialSummary,
        this.regional,
    });

    factory DataClass.fromJson(Map<String, dynamic> json) => DataClass(
        summary: Summary.fromJson(json["summary"]),
        unofficialSummary: List<UnofficialSummary>.from(json["unofficial-summary"].map((x) => UnofficialSummary.fromJson(x))),
        regional: List<Regional>.from(json["regional"].map((x) => Regional.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "summary": summary.toJson(),
        "unofficial-summary": List<dynamic>.from(unofficialSummary.map((x) => x.toJson())),
        "regional": List<dynamic>.from(regional.map((x) => x.toJson())),
    };
}

class Regional {
    String loc;
    int confirmedCasesIndian;
    int discharged;
    int deaths;
    int confirmedCasesForeign;
    int totalConfirmed;

    Regional({
        this.loc,
        this.confirmedCasesIndian,
        this.discharged,
        this.deaths,
        this.confirmedCasesForeign,
        this.totalConfirmed,
    });

    factory Regional.fromJson(Map<String, dynamic> json) => Regional(
        loc: json["loc"],
        confirmedCasesIndian: json["confirmedCasesIndian"],
        discharged: json["discharged"],
        deaths: json["deaths"],
        confirmedCasesForeign: json["confirmedCasesForeign"],
        totalConfirmed: json["totalConfirmed"],
    );

    Map<String, dynamic> toJson() => {
        "loc": loc,
        "confirmedCasesIndian": confirmedCasesIndian,
        "discharged": discharged,
        "deaths": deaths,
        "confirmedCasesForeign": confirmedCasesForeign,
        "totalConfirmed": totalConfirmed,
    };
}

class Summary {
    int total;
    int confirmedCasesIndian;
    int confirmedCasesForeign;
    int discharged;
    int deaths;
    int confirmedButLocationUnidentified;

    Summary({
        this.total,
        this.confirmedCasesIndian,
        this.confirmedCasesForeign,
        this.discharged,
        this.deaths,
        this.confirmedButLocationUnidentified,
    });

    factory Summary.fromJson(Map<String, dynamic> json) => Summary(
        total: json["total"],
        confirmedCasesIndian: json["confirmedCasesIndian"],
        confirmedCasesForeign: json["confirmedCasesForeign"],
        discharged: json["discharged"],
        deaths: json["deaths"],
        confirmedButLocationUnidentified: json["confirmedButLocationUnidentified"],
    );

    Map<String, dynamic> toJson() => {
        "total": total,
        "confirmedCasesIndian": confirmedCasesIndian,
        "confirmedCasesForeign": confirmedCasesForeign,
        "discharged": discharged,
        "deaths": deaths,
        "confirmedButLocationUnidentified": confirmedButLocationUnidentified,
    };
}

class UnofficialSummary {
    String source;
    int total;
    int recovered;
    int deaths;
    int active;

    UnofficialSummary({
        this.source,
        this.total,
        this.recovered,
        this.deaths,
        this.active,
    });

    factory UnofficialSummary.fromJson(Map<String, dynamic> json) => UnofficialSummary(
        source: json["source"],
        total: json["total"],
        recovered: json["recovered"],
        deaths: json["deaths"],
        active: json["active"],
    );

    Map<String, dynamic> toJson() => {
        "source": source,
        "total": total,
        "recovered": recovered,
        "deaths": deaths,
        "active": active,
    };
}

下面是api服务调用

class ApiServices {

  static const String url = 'https://api.rootnet.in/covid19-in/stats/latest';
static Future<Data> getDataFromAPI() async {
    try {
      final response = await http.get(url);
      if (200 == response.statusCode) {
        final Data data = dataFromJson(response.body);
        //regionalData = data.data.regional;
        // And make the return value as List<Regional> or send the complete object which is benefiicial
        return data;
      } else {
        return null;
      }
    } catch (e) {
      return null;
    }
  }
}

最后是如下所示的ui:

oid main()=> runApp(Statewise());



class Statewise extends StatefulWidget {
  @override
  _StatewiseState createState() => _StatewiseState();
}

class _StatewiseState extends State<Statewise> {
  Data responseData;
  bool _isloading;
  List<Regional> regionalData;

  @override
  void initState() {
    super.initState();

    _isloading = true;
    ApiServices.getDataFromAPI().then((data) {
      // you can  initially get the regional data from the getDataFromAPI() method
      setState(() {
        responseData = data;// here you get the complete object 
              // here you can also get the  regional data only  from below code 
           //regionalData = data.data.regional; and assign to the List<Regional>
        _isloading = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    if (_isloading) {
      return Container(
        child: Center(
          child: CircularProgressIndicator(),
        ),
      );
    } else {
      return MaterialApp(
              home: Scaffold(
          body: ListView.builder(
              itemCount: responseData.data.regional == null ? 0 : responseData.data.regional.length,
              itemBuilder: (context, index) {
                var region = responseData.data.regional[index];
                //Data datas = _datafromApi[index];
                //print(datas?.toString() ?? "Empty");
                return ListTile(
                    title: Text(
                  region.loc.toString(),
                  style: TextStyle(color: Colors.black),
                ));
              }),
        ),
      );
    }
  }
}

退房,让我知道您是否有问题。