Flutter:“ NoSuchMethodError”不是字符串类型的子类型

时间:2018-09-13 09:41:10

标签: dart flutter

我目前有一个应用程序可以通过JSON提取提供程序列表。

这是JSON文件。

hospitals.json

{
"Johor": [
    {
      "Name": "KLINIK TAJ (MUAR)",
      "TPA Customer ID": "1168",
      "Organization Type": "Clinic",
      "Organization Subtype": "GP",
      "Street (Billing)": "35a, jalan abdullah ",
      "City (Billing)": "muar",
      "Postal Code (Billing)": "84000",
      "State (Billing)": "Johor",
      "Country (Billing)": "Malaysia",
      "Coordinates": {
        "Latitude": "2.041875",
        "Longitude": "102.568235"
      }
    },
    {
      "Name": "KLINIK TAJ (PAGOH)",
      "TPA Customer ID": "1169",
      "Organization Type": "Clinic",
      "Organization Subtype": "GP",
      "Street (Billing)": "100 Main Road Pagoh",
      "City (Billing)": "Muar",
      "Postal Code (Billing)": "84600",
      "State (Billing)": "Johor",
      "Country (Billing)": "Malaysia",
      "Coordinates": {
        "Latitude": "2.148342",
        "Longitude": "102.771002"
      }
    }
  ],
  "Kedah": [
    {
      "Name": "KLINIK TAN",
      "TPA Customer ID": "8423",
      "Organization Type": "Clinic",
      "Organization Subtype": "GP",
      "Street (Billing)": "62 Jalan Raya",
      "City (Billing)": "Kulim",
      "Postal Code (Billing)": "9000",
      "State (Billing)": "Kedah",
      "Coordinates": {
        "Latitude": "5.366739",
        "Longitude": "100.553988"
      }
    },
    {
      "Name": "KLINIK SHAN",
      "TPA Customer ID": "1685",
      "Organization Type": "Clinic",
      "Organization Subtype": "GP",
      "Street (Billing)": "L. C. 19, Jalan Lunas,",
      "City (Billing)": "Padang Serai",
      "Postal Code (Billing)": "9000",
      "State (Billing)": "Kedah",
      "Coordinates": {
        "Latitude": "5.402193",
        "Longitude": "100.555209"
      }
    }
  ]
}

这是JSON的模型类

new_accounts_model.dart

class Johor {

  List<AccountInfo> accountinfo;

  Johor({this.accountinfo});

  factory Johor.fromJson(Map<String, dynamic> json){

    var accJson = json["Johor"] as List;
    List<AccountInfo> accList = accJson.map((i) => AccountInfo.fromJson(i)).toList();

    return Johor(
        accountinfo: accList
    );
  }
}

class AccountInfo{

  String name;
  String id;
  String orgtype;
  String subtype;
  String street;
  String city;
  String country;
  Coordinates coordinates;

  AccountInfo({this.name, this.id, this.orgtype, this.subtype, this.street, this.city, this.country, this.coordinates});

  factory AccountInfo.fromJson(Map<String, dynamic> json){

    return AccountInfo(
        name: json["Name"],
        id: json["TPA Customer ID"],
        orgtype: json["Organization Type"],
        subtype: json["Organization Subtype"],
        street: json["Street (Billing)"],
        city: json["City (Billing)"],
        country: json["State (Billing)"],
        coordinates: Coordinates.fromJson(json["Coordinate"])
    );
  }
}

class Coordinates{

  String lat;
  String lng;

  Coordinates({this.lat, this.lng});

  factory Coordinates.fromJson(Map<String, dynamic> json){

    return Coordinates(
        lat: json["Latitude"],
        lng: json["Longitude"]
    );
  }
}

这是用于导出JSON文件的dart文件。

list.dart

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
import 'dart:convert';
import 'package:emas_app/model/new_accounts_model.dart';

Future<String> _loadAsset() async{
  return await rootBundle.loadString('Assets/hospitals.json');
}

//Not working future
Future<Johor> loadJohor() async{

  final response = await _loadAsset();
  final jsonResponse = json.decode(response);

  Johor johor = new Johor.fromJson(jsonResponse);

  return johor;
}

class ProviderList extends StatefulWidget {

  @override
  ListState createState() {
    return new ListState();
  }
}

class ListState extends State<ProviderList> {

  @override
  Widget build(BuildContext context) {

    List<Widget> widgets = [];

    launchMapUrl(String lat, String lng) async{
      String geoUri = "https://maps.google.com/maps?q=loc:$lat,$lng";
      if (await canLaunch(geoUri)) {
        print("Can launch");
        await launch(geoUri);
      } else {
        print("Could not launch");
        throw 'Could not launch Maps';
      }
    }

    //method to bring out dialog
    makeDialog(String address){
      showDialog(
          context: context,
          builder: (_) => new SimpleDialog(
            contentPadding: EdgeInsets.only(left: 30.0, top: 30.0),
            children: <Widget>[
              new Text("Address: $address",
                style: TextStyle(
                  fontWeight: FontWeight.bold
                ),
              ),
              new ButtonBar(
                children: <Widget>[
                  new IconButton(
                      icon: Icon(Icons.close),
                      onPressed: (){
                        Navigator.pop(context);
                      }
                      )
                ],
              )
            ],
          )
      );
    }

    widgets.add(new ExpansionTile(
        title: new Text("Not working state"),
        children: <Widget>[
          new FutureBuilder<Johor>(
              future: loadJohor(),
              builder: (context, snapshot){
                if(snapshot.hasData){

                  return new ListView.builder(
                      shrinkWrap: true,
                      itemCount: snapshot.data.accountinfo.length,
                      itemBuilder: (context, index){

                        String username = snapshot.data.accountinfo[index].name;
                        String address = snapshot.data.accountinfo[index].street;
                        String lat = snapshot.data.accountinfo[index].coordinates.lat;
                        String lng = snapshot.data.accountinfo[index].coordinates.lng;

                        return new ListTile(
                            title: new Text(username),
                            trailing: new Row(
                              mainAxisSize: MainAxisSize.min,
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: <Widget>[
                                new IconButton(
                                    icon: Icon(Icons.info),
                                    onPressed: (){
                                      makeDialog(address);
                                    }
                                ),
                                new IconButton(
                                    icon: Icon(Icons.directions),
                                    onPressed: (){
                                      launchMapUrl(lat, lng);
                                    }
                                )
                              ],
                            )
                        );
                      });
                }else if(snapshot.hasError){
                  return new Center(
                    child: new Text(snapshot.error),
                  );
                }
              })
        ]
    ));

    //empty list
    widgets.add(new ExpansionTile(
        title: new Text("Pahang")));

    return new Scaffold(
      appBar: new AppBar(title: new Text("Providers")),
      body: new Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: widgets,
      )
    );
  }
}

这是当前面临的错误:

NosuchMethod

正如标题所述,错误是其NoSuchMethodError。因此,我不确定到底是什么导致了此错误。

我目前的猜测是我没有正确执行Model类,但可能还有其他原因。

在这种情况下,我真的可以使用一些帮助。

1 个答案:

答案 0 :(得分:3)

您为Coordinates使用了错误的密钥。 您应该使用Coordinates作为json中密钥的名称。但是您在方法factory AccountInfo.fromJson

中使用了Coordinate

更新该方法的最后一行

coordinates: Coordinates.fromJson(json["Coordinates"])