gpt4 book ai didi

dart - Flutter: 'NoSuchMethodError' 不是 String 类型的子类型

转载 作者:IT老高 更新时间:2023-10-28 12:45:09 25 4
gpt4 key购买 nike

我目前有一个通过 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 类,但它可能是其他问题。

在这种情况下,我真的需要一些帮助。

最佳答案

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

中使用坐标

更新该方法的最后一行

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

关于dart - Flutter: 'NoSuchMethodError' 不是 String 类型的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52310940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com