Flutter - 如何计算listview上每个产品的平均评分数
Posted
技术标签:
【中文标题】Flutter - 如何计算listview上每个产品的平均评分数【英文标题】:Fluter - How to calculate the average number of rating for each product on listview 【发布时间】: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)),
); ```
【问题讨论】:
【参考方案1】:好的,所以我能够找到一个完美的方法来获得平均评分,方法是加入两个表,即“产品”和“评分”表,并将“产品”的 ID 与“”的 ID 进行比较产品”存储在“评级”表中,并通过以下 sql 查询获得总平均评级:
$sql = "product.id, AVG(rating.rated) AS rated FROM product LEFT JOIN rating ON product.id = rating.product_id GROUP BY product.id",
【讨论】:
以上是关于Flutter - 如何计算listview上每个产品的平均评分数的主要内容,如果未能解决你的问题,请参考以下文章
Flutter - 如何访问 ListView.builder 中的一个元素?
如何使用 Flutter 从 Firestore 中的 ListView 获得无限滚动