我有一些图像和详细信息的列表视图,该列表视图显示了虚拟数据,但工作正常,但如果我尝试调用API并初始化了虚假数据的值,但该列表视图甚至未显示在屏幕上。我也从API直到var restaurants = value['restaurants'][0];
都获取数据,但是我无法访问列表视图中的数据,没有错误消息,Flutter Doctor没有问题API调用
Future<String> getRestaurantList() async {
ProgressDialog dialog = CustomDialogs().showLoadingProgressDialog(context);
var response = await http.post(
Urls.HOME_RESTAURANTS,
headers: {"Content-Type": "application/json"},
body: json.encode({
"line1": "lat_11.2717278\$75.7775994",
"category": "all",
"term": "null",
}),
);
Map<String, dynamic> value = json.decode(response.body);
if (response.statusCode == 200) {
dialog.dismissProgressDialog(context);
try {
print("response " + response.body.toString());
var message = value['msg'];
var rescount = value['count'];
if (message == "Exist") {
var restaurants = value['restaurants'][0];
for (int i = 0; i < restaurants.length; i++) {
var data = restaurants[i];
print("response"+ data);
HomeRestauarantList.add(HomeRestauarantModel.fromJson(data));
}
} else {
final snackBar = SnackBar(
content: Text("No Restaurants Available in the Location"));
_scaffoldKey.currentState.showSnackBar(snackBar);
}
} catch (e) {
e.toString();
print(e.toString());
}
}
}
查看
Widget restList = Container(
width: double.infinity,
child: Column(
children: <Widget>[
Container(
child: ListView.separated(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
separatorBuilder: (BuildContext context, int index) =>
const Divider(),
itemCount: HomeRestauarantList.length,
itemBuilder: (BuildContext context, int index) {
HomeRestauarantModel data = HomeRestauarantList[index];
return Container(
width: double.infinity,
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => RestaurantScreen()),
);
},
child: Padding(
padding: EdgeInsets.all(16),
child: Container(
child: Row(
children: <Widget>[
Expanded(
flex: 0,
child: Image.network(
data.logo,
height: 80,
fit: BoxFit.fitWidth,
),
//radius: 52.5,
),
Expanded(
flex: 1,
child: Padding(
padding: EdgeInsets.all(16),
child: Row(
children: <Widget>[
Expanded(
flex: 0,
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.all(0),
child: Text(
data.name,
style: TextStyle(
fontSize: 15,
color: Colors.black,
),
textAlign: TextAlign.left,
),
),
Padding(
padding: EdgeInsets.only(top: 5),
child: Text(
data.tag_line,
style: TextStyle(
fontSize: 12,
color: Colors.grey,
),
textAlign: TextAlign.left,
),
),
Padding(
padding: EdgeInsets.only(top: 5),
child: Text(
"20% Offer above ₹130",
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: const Color(0xFFCBB032),
),
textAlign: TextAlign.left,
),
),
],
),
)
],
),
),
),
],
),
),
),
],
),
),
),
),
);
}),
),
],
),
);
型号
class HomeRestauarantModel {
String res_id;
String name;
String logo;
String status;
String tag_line;
String min_prepration_time;
HomeRestauarantModel({
this.res_id,
this.name,
this.logo,
this.status,
this.tag_line,
this.min_prepration_time,
});
HomeRestauarantModel.fromJson(json)
: res_id = json['res_id'].toString(),
name = json['name'],
logo = json['logo'].toString(),
status = json['status'].toString(),
tag_line = json['tag_line'].toString(),
min_prepration_time = json['min_prepration_time'].toString();
}
或使用'FuretureBuilder'更新'HomeRestauarantList'的构建。或四处看看Bloc,提供者模式。