_TypeError(型別 '_InternalLinkedHashMap<String, dynamic>' 不是型別 'List' 的子型別)
這是我的http方法類:
List<Property>? areaview;
Future<void> fetcharea() async {
final response = await http.get(
Uri.parse('https://arz-e-nafees.nafeessolutions.com/public/api/view'));
if (response.statusCode == 200) {
var property = (json.decode(response.body));
Areaview viewarea = Areaview.fromJson(property);
areaview = viewarea.properties;
return property;
} else {
throw Exception('Unexpected error occured!');
}
}
這是我的模型課:
// To parse this JSON data, do
//
// final areaview = areaviewFromJson(jsonString);
import 'dart:convert';
Areaview areaviewFromJson(String str) => Areaview.fromJson(json.decode(str));
String areaviewToJson(Areaview data) => json.encode(data.toJson());
class Areaview {
Areaview({
required this.properties,
});
List<Property> properties;
factory Areaview.fromJson(Map<String, dynamic> json) => Areaview(
properties: List<Property>.from(
json["properties"].map((x) => Property.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"properties": List<dynamic>.from(properties.map((x) => x.toJson())),
};
}
class Property {
Property({
required this.propertyImage,
required this.propertyTitle,
required this.locationCity,
required this.locationArea,
required this.propertyDescription,
required this.propertyPrice,
});
String? propertyImage;
String propertyTitle;
String locationCity;
String locationArea;
String propertyDescription;
String propertyPrice;
factory Property.fromJson(Map<String, dynamic> json) => Property(
propertyImage:
json["property_image"] == null ? null : json["property_image"],
propertyTitle: json["property_title"],
locationCity: json["location_city"],
locationArea: json["location_area"],
propertyDescription: json["property_description"],
propertyPrice: json["property_price"],
);
Map<String, dynamic> toJson() => {
"property_image": propertyImage == null ? null : propertyImage,
"property_title": propertyTitle,
"location_city": locationCity,
"location_area": locationArea,
"property_description": propertyDescription,
"property_price": propertyPrice,
};
}
這是我未來的建設者:
FutureBuilder(
future: fetcharea(),
builder: (context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
} else {
return VerticalCards(snapshot.data);
}
},
),
這是例外的螢屏截圖

從http方法回傳后我得到的資料型別:

問題是當我嘗試回傳 Areaview 型別時我回傳,然后我無法回傳垂直卡片中的屬性,當我回傳屬性型別時,我無法回傳 Areaview。
編輯:垂直卡類
// ignore_for_file: prefer_const_constructors, must_be_immutable, use_key_in_widget_constructors, sized_box_for_whitespace
import 'package:arzenafees/Components/Constants.dart';
import 'package:arzenafees/model/areaview.dart';
import 'package:arzenafees/services/areaviewapi.dart';
import 'package:flutter/material.dart';
class VerticalCards extends StatelessWidget {
List<Areaview> areaData;
VerticalCards(this.areaData);
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(color: Constants.colorSecond),
height: MediaQuery.of(context).size.height,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: areaData.length,
itemBuilder: (context, index) {
final String? image = areaData[index] as String;
return Container(
// width: MediaQuery.of(context).size.width * 0.6,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: SizedBox(
width: 300,
height: 180,
child: GestureDetector(
onTap: () {},
child: Stack(alignment: Alignment.bottomLeft, children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(15.0)),
image: DecorationImage(
image: NetworkImage(image!),
fit: BoxFit.fill,
),
),
),
]),
)),
),
));
},
),
);
}
}
uj5u.com熱心網友回復:
試試看有什么問題告訴我
Future fetcharea() async {
final response = await http.get(
Uri.parse('https://arz-e-nafees.nafeessolutions.com/public/api/view'));
if (response.statusCode == 200) {
return areaviewFromJson(response.body).properties;
} else {
throw Exception('Unexpected error occured!');
}
}
FutureBuilder(
future: fetcharea(),
builder: (context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
} else {
final data = snapshot.data
return VerticalCards(data);
}
},
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/493475.html
