From eca2b6324ba1ef8000046249e00c8c3d47ae4be0 Mon Sep 17 00:00:00 2001 From: Reimar Date: Fri, 13 Sep 2024 12:02:22 +0200 Subject: [PATCH] Fix errors on review page, show text when no reviews --- Mobile/lib/reviewlist.dart | 117 ++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 53 deletions(-) diff --git a/Mobile/lib/reviewlist.dart b/Mobile/lib/reviewlist.dart index 325eb8a..13f5fd0 100644 --- a/Mobile/lib/reviewlist.dart +++ b/Mobile/lib/reviewlist.dart @@ -14,10 +14,14 @@ class ReviewListPage extends StatefulWidget { } class _ReviewListState extends State { - List users = []; + List _users = []; models.User? _getReviewUser(models.Review review) { - return users.firstWhere((user) => user.id == review.userId); + try { + return _users.firstWhere((user) => user.id == review.userId); + } catch(e) { + return null; + } } @override @@ -27,14 +31,18 @@ class _ReviewListState extends State { final arg = ModalRoute.of(context)!.settings.arguments as models.ReviewList; final reviews = arg.reviews; + if (reviews.isEmpty) { + return; + } + final userIds = reviews.map((review) => review.userId).toSet().toList(); final response = await api.request(context, api.ApiService.auth, 'GET', '/api/Users/UsersByIds?userIds=' + userIds.join(','), null); if (response == null) return; - debugPrint('response: ' + response); - - users = (jsonDecode(response) as List).map((user) => models.User.fromJson(user)).toList(); + setState(() { + _users = (jsonDecode(response) as List).map((user) => models.User.fromJson(user)).toList(); + }); } @override @@ -47,56 +55,59 @@ class _ReviewListState extends State { selectedIndex: -1, body: Scaffold( backgroundColor: const Color(0xFFF9F9F9), - body: SingleChildScrollView(child: Container( - decoration: const BoxDecoration(color: Color(0xFFF9F9F9)), - width: MediaQuery.of(context).size.width, - padding: const EdgeInsets.all(20.0), - child: Column(children: - reviews.map((review) => Container( - width: double.infinity, - padding: const EdgeInsets.all(20), - margin: const EdgeInsets.only(bottom: 10), - decoration: const BoxDecoration( - boxShadow: [ - BoxShadow( - color: Color(0x20000000), - offset: Offset(0,1), - blurRadius: 4, + body: reviews.isEmpty + ? const Center(child: Text('No reviews yet. Be the first to review this place')) + : SingleChildScrollView(child: Container( + decoration: const BoxDecoration(color: Color(0xFFF9F9F9)), + width: MediaQuery.of(context).size.width, + padding: const EdgeInsets.all(20.0), + child: Column(children: [ + for (final review in reviews) + Container( + width: double.infinity, + padding: const EdgeInsets.all(20), + margin: const EdgeInsets.only(bottom: 10), + decoration: const BoxDecoration( + boxShadow: [ + BoxShadow( + color: Color(0x20000000), + offset: Offset(0,1), + blurRadius: 4, + ), + ], + color: Colors.white, ), - ], - color: Colors.white, - ), - child: Row( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - const Padding( - padding: EdgeInsets.only(top: 3), - child: Icon(Icons.rate_review, color: Colors.purple, size: 36), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Padding( + padding: EdgeInsets.only(top: 3), + child: Icon(Icons.rate_review, color: Colors.purple, size: 36), + ), + const SizedBox(width: 20), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text(review.title, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)), + Text(review.content), + const SizedBox(height: 10), + if (review.image != null) Image.network(review.image!.imageUrl, height: 200), + if (review.image != null) const SizedBox(height: 15), + Row(children: [ + for (var i = 0; i < review.rating; i++) const Icon(Icons.star, color: Colors.yellow), + for (var i = review.rating; i < 5; i++) const Icon(Icons.star_border), + ]), + const SizedBox(height: 10), + Text('Submitted by ' + (_getReviewUser(review)?.username ?? ''), style: const TextStyle(color: Colors.grey, fontSize: 12)), + ], + ), + ), + ], ), - const SizedBox(width: 20), - Expanded( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text(review.title, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)), - Text(review.content), - const SizedBox(height: 10), - if (review.image != null) Image.network(review.image!.imageUrl, height: 200), - if (review.image != null) const SizedBox(height: 15), - Row(children: [ - for (var i = 0; i < review.rating; i++) const Icon(Icons.star, color: Colors.yellow), - for (var i = review.rating; i < 5; i++) const Icon(Icons.star_border), - ]), - const SizedBox(height: 10), - Text('Submitted by ' + (_getReviewUser(review)?.username ?? ''), style: const TextStyle(color: Colors.grey, fontSize: 12)), - ], - ), - ), - ], - ), - )).toList(), - ), - )), + ), + ]), + )), floatingActionButton: FloatingActionButton( onPressed: () async { if (!await api.isLoggedIn(context)) {