Fix errors on review page, show text when no reviews

This commit is contained in:
Reimar 2024-09-13 12:02:22 +02:00
parent 90fccde9e6
commit eca2b6324b
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268

View File

@ -14,10 +14,14 @@ class ReviewListPage extends StatefulWidget {
} }
class _ReviewListState extends State<ReviewListPage> { class _ReviewListState extends State<ReviewListPage> {
List<models.User> users = []; List<models.User> _users = [];
models.User? _getReviewUser(models.Review review) { 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 @override
@ -27,14 +31,18 @@ class _ReviewListState extends State<ReviewListPage> {
final arg = ModalRoute.of(context)!.settings.arguments as models.ReviewList; final arg = ModalRoute.of(context)!.settings.arguments as models.ReviewList;
final reviews = arg.reviews; final reviews = arg.reviews;
if (reviews.isEmpty) {
return;
}
final userIds = reviews.map((review) => review.userId).toSet().toList(); 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); final response = await api.request(context, api.ApiService.auth, 'GET', '/api/Users/UsersByIds?userIds=' + userIds.join(','), null);
if (response == null) return; if (response == null) return;
debugPrint('response: ' + response); setState(() {
_users = (jsonDecode(response) as List<dynamic>).map((user) => models.User.fromJson(user)).toList();
users = (jsonDecode(response) as List<dynamic>).map((user) => models.User.fromJson(user)).toList(); });
} }
@override @override
@ -47,56 +55,59 @@ class _ReviewListState extends State<ReviewListPage> {
selectedIndex: -1, selectedIndex: -1,
body: Scaffold( body: Scaffold(
backgroundColor: const Color(0xFFF9F9F9), backgroundColor: const Color(0xFFF9F9F9),
body: SingleChildScrollView(child: Container( body: reviews.isEmpty
decoration: const BoxDecoration(color: Color(0xFFF9F9F9)), ? const Center(child: Text('No reviews yet. Be the first to review this place'))
width: MediaQuery.of(context).size.width, : SingleChildScrollView(child: Container(
padding: const EdgeInsets.all(20.0), decoration: const BoxDecoration(color: Color(0xFFF9F9F9)),
child: Column(children: width: MediaQuery.of(context).size.width,
reviews.map((review) => Container( padding: const EdgeInsets.all(20.0),
width: double.infinity, child: Column(children: [
padding: const EdgeInsets.all(20), for (final review in reviews)
margin: const EdgeInsets.only(bottom: 10), Container(
decoration: const BoxDecoration( width: double.infinity,
boxShadow: [ padding: const EdgeInsets.all(20),
BoxShadow( margin: const EdgeInsets.only(bottom: 10),
color: Color(0x20000000), decoration: const BoxDecoration(
offset: Offset(0,1), boxShadow: [
blurRadius: 4, BoxShadow(
color: Color(0x20000000),
offset: Offset(0,1),
blurRadius: 4,
),
],
color: Colors.white,
), ),
], child: Row(
color: Colors.white, crossAxisAlignment: CrossAxisAlignment.start,
), children: [
child: Row( const Padding(
crossAxisAlignment: CrossAxisAlignment.start, padding: EdgeInsets.only(top: 3),
children: [ child: Icon(Icons.rate_review, color: Colors.purple, size: 36),
const Padding( ),
padding: EdgeInsets.only(top: 3), const SizedBox(width: 20),
child: Icon(Icons.rate_review, color: Colors.purple, size: 36), 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( floatingActionButton: FloatingActionButton(
onPressed: () async { onPressed: () async {
if (!await api.isLoggedIn(context)) { if (!await api.isLoggedIn(context)) {