【发布时间】:2020-07-23 17:54:43
【问题描述】:
我有两个数据库表,一个包含“产品”列表,另一个包含产品的“评级”。当用户提交他们的评分时,用户 ID 与用户评分和评论一起发送到“评分”表中。我的 PHP REST API 如下所示,它根据内容 ID 3、5、4、2 等返回用户评分列表。如何使用 SmoothStarRating 在我的颤动代码中的列表视图中显示每个产品的所有用户的平均评分.下面是我的 Flutter 代码和我的 PHP rest api:
require 'database.php';
$db_connection = new Database();
$conn = $db_connection->dbConnection();
if(isset($_GET['id']))
{
//IF HAS ID PARAMETER
$post_id = filter_var($_GET['id'], FILTER_VALIDATE_INT,[
'options' => [
'default' => 'all_posts',
'min_range' => 1
]
]);
}
else{
$post_id = 'all_posts';
}
$sql = "SELECT * FROM `Ratings` where deal_id = '$post_id' ";
$stmt = $conn->prepare($sql);
$stmt->execute();
if($stmt->rowCount() > 0){
// CREATE POSTS ARRAY
$posts_array = [];
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$post_data = [$row['ratings']];
$post_data1 = implode(" ",$post_data);
array_push($posts_array, $post_data1);
}
echo json_encode($posts_array, JSON_NUMERIC_CHECK);
}
else{
echo json_encode(['message'=>'No post']);
}
?>
Future<String> getRatings() async {
setState(() {
isLoading = true;
});
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
print('connected');
debugPrint("emirate connect");
String url =
"http://mysite/shopper_api/view_rating.php?id=" +
content.id;
var res = await http
.get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
resRating = json.decode(res.body);
debugPrint("rating url:$url");
setState(() {
data = resRating;
isLoading = false;
});
print(resRating);
debugPrint("ratings:$resRating");
return "Sucess";
} else {
throw Exception('Failed to load profile');
}
} on SocketException catch (_) {
print('not connected');
setState(() => isLoading = false);
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return CupertinoAlertDialog(
//title: Text('Internet Error!'),
content: Text(
'Check your internet connection and press Ok.',
style: TextStyle(fontFamily: 'Montserrat'),
),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (BuildContext ctx) => Maps(content)));
},
child: Text(
'Ok',
style:
TextStyle(color: colorPink, fontFamily: 'Montserrat'),
)),
],
);
});
}
}
@override
Widget build(BuildContext context) {
// TODO: implement build
var rating = 1.0;
return Scaffold(
child:Padding(
padding: const EdgeInsets.only(
bottom: 0),
child: SmoothStarRating(
allowHalfRating: false,
onRatingChanged: (v) {
setState(() {
this.rating = v;
});
},
starCount: 5,
rating: rating,
size: 20.0,
filledIconData: Icons.star,
halfFilledIconData:
Icons.star_half,
color: colorGreen,
borderColor: colorGreen,
spacing: 0.0)),
);} ```
【问题讨论】: