【发布时间】:2020-09-17 02:37:01
【问题描述】:
嗨,我是 Flutter 和 dart 的新手,我有这个问题,即页面没有显示数据,即使 listView.seperator 中有数据,当我检查 dubugger 时,我会在 snapshot.data[index] 中获取数据.finishedProductId 或finishedProductName 或categoryShortName,但调试器完成它显示空白页,甚至删除除Text 之外的所有内容,仍然显示空白页我正在提供代码,请帮助我。我提前给我的代码如下。为了安全起见,我用 start 标记了我的网站链接。对此感到抱歉。
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
class DetailsPage extends StatefulWidget {
final String itemCode;
DetailsPage({Key key, this.itemCode}) : super(key: key);
@override
_DetailsPageState createState() => _DetailsPageState();
}
class _DetailsPageState extends State<DetailsPage> {
// ignore: missing_return
Future<List<ItemDetails>> _getItemDetails() async {
final response = await http.get(
'https://www.****.com/appfiles/getprodcontent.php?getContent=' +
this.widget.itemCode );
var jsonData = jsonDecode(response.body);
print(jsonData);
List<ItemDetails> itemDet = [];
for (var it in jsonData) {
ItemDetails iDet = ItemDetails(
it['imgPath'],
it['finishedProductId'],
it["finishedProductName"],
it["categoryShortName"],
);
itemDet.add(iDet);
}
print(itemDet.length);
return itemDet;
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blueGrey[200],
appBar: AppBar(
title: Text("Product Details of " + this.widget.itemCode),
),
body: Container(
child: FutureBuilder(
initialData: [],
future: _getItemDetails(),
// ignore: missing_return
builder: (BuildContext context, AsyncSnapshot snapshot) {
//if(snapshot.connectionState == ConnectionState.done){
//if (snapshot.data == null) {
// return Container(
// child: Center(
// child: Text('Loading...'),
/// ));
// } else {
return Column(
children: <Widget> [
SizedBox(
height:200,
child: ListView.seperated(
padding: const EdgeInsets.all(8),
itemCount: snapshot.data.length,
// ignore: missing_return
itemBuilder: (BuildContext context, int index) {
Container (
child:
Center(child: Text("finished product id") ,),
);
Container(
height: 50,
color: Colors.amber[200],
child: Center(
child: CircleAvatar(
radius: 50.0,
backgroundImage: NetworkImage(
"https://www.shardaudyogmandir.com/assets/thumbs/javaschatni.png")
!= null ?NetworkImage("https://www.****.com/assets/thumbs/***.png") : Container() ,
),
),
);
Container(
height: 50,
color: Colors.amber[200],
child: Center(
child: Text(snapshot.data[index].finishedProductId != null ? snapshot.data[index].finishedProductId: "",
style: TextStyle(
color: Colors.black,
fontSize: 20,
fontWeight: FontWeight.bold)),
),
);
Container(
height: 50,
color: Colors.amber[200],
child: Center(
child: Text(snapshot.data[index].finishedProductName != null ? snapshot.data[index].finishedProductName: "",
style: TextStyle(color: Colors.black)),
),
);
Container(
height: 50,
color: Colors.amber[200],
child: Center(
child: Text(snapshot.data[index].categoryShortName != null ? snapshot.data[index].categoryShortName : "",
style: TextStyle(color: Colors.black)),
),
);
Container(
height: 50,
color: Colors.amber[200],
child: Center(
child: RaisedButton(
color: Colors.green[700],
onPressed: () {
//print("press view details");
},
child: const Text('Add To Cart',
style: TextStyle(
fontSize: 20,
color: Colors.white)),
),
),
);
},
//separatorBuilder: (BuildContext context, int index) =>
//const Divider(),
),
)
],
);
//}
//}
}
)
),
);
}
}
class ItemDetails {
final String finishedProductId;
final String finishedProductName;
final String categoryShortName;
//final String rawMaterialName;
final String imgPath;
ItemDetails(this.imgPath, this.finishedProductId, this.finishedProductName,
this.categoryShortName);
}
【问题讨论】:
-
尝试将您的
ListView.separated包裹在Container中,并给它正确的height和width -
我试过你的选择,但没有运气......仍然给出相同的空白页......