Skip to content

Commit

Permalink
fix: future must not be created during build (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
boxdot authored Dec 13, 2024
1 parent b2f8f82 commit 5d8af9b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class TextMessageTile extends StatelessWidget {

Widget _avatar(BuildContext context) {
return FutureUserAvatar(
profile: context.coreClient.user
profile: () => context.coreClient.user
.userProfile(userName: contentFlight.last.sender),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class _AddMembersState extends State<AddMembers> {
final contact = contacts[index];
return ListTile(
leading: FutureUserAvatar(
profile: context.coreClient.user
profile: () => context.coreClient.user
.userProfile(userName: contact.userName),
),
title: Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ class ConnectionDetails extends StatelessWidget {
children: [
const SizedBox(height: _padding),
FutureUserAvatar(
size: 64,
profile: coreClient.user.userProfile(
userName: conversation.conversationType.when(
unconfirmedConnection: (e) => e,
connection: (e) => e,
group: () => ''),
)),
size: 64,
profile: () => coreClient.user.userProfile(
userName: conversation.conversationType.when(
unconfirmedConnection: (e) => e,
connection: (e) => e,
group: () => ''),
),
),
const SizedBox(height: _padding),
Text(
conversation.conversationType.when(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class _GroupDetailsState extends State<GroupDetails> {
return ListTile(
leading: FutureUserAvatar(
size: 24,
profile: coreClient.user
profile: () => coreClient.user
.userProfile(userName: members[index]),
),
title: Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MemberDetails extends StatelessWidget {
const SizedBox(height: _padding),
FutureUserAvatar(
size: 64,
profile: context.coreClient.user
profile: () => context.coreClient.user
.userProfile(userName: memberUsername),
),
const SizedBox(height: _padding),
Expand Down
60 changes: 27 additions & 33 deletions app/lib/elements.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

import 'dart:typed_data';
import 'dart:ui';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:prototype/core/api/types.dart';
import 'package:prototype/styles.dart';
Expand All @@ -21,7 +21,7 @@ IconButton appBarBackButton(BuildContext context) {
}

class FutureUserAvatar extends StatefulWidget {
final Future<UiUserProfile?> profile;
final AsyncValueGetter<UiUserProfile?> profile;
final VoidCallback? onPressed;
final double size;

Expand All @@ -37,29 +37,29 @@ class FutureUserAvatar extends StatefulWidget {
}

class _FutureUserAvatarState extends State<FutureUserAvatar> {
late final Future<UiUserProfile?> _profileFuture;

@override
void initState() {
_profileFuture = widget.profile();
super.initState();
}

@override
Widget build(BuildContext context) {
return FutureBuilder<UiUserProfile?>(
future: widget.profile,
builder: (context, snapshot) {
if (snapshot.hasData) {
return UserAvatar(
username: snapshot.data!.userName,
size: widget.size,
image: snapshot.data!.profilePictureOption,
onPressed: widget.onPressed);
} else {
return UserAvatar(
username: " ",
size: widget.size,
image: null,
onPressed: widget.onPressed);
}
});
future: _profileFuture,
builder: (context, snapshot) => UserAvatar(
username: snapshot.data?.userName ?? " ",
image: snapshot.data?.profilePictureOption,
size: widget.size,
onPressed: widget.onPressed,
),
);
}
}

class UserAvatar extends StatefulWidget {
class UserAvatar extends StatelessWidget {
final String username;
final double size;
final Uint8List? image;
Expand All @@ -73,32 +73,26 @@ class UserAvatar extends StatefulWidget {
this.onPressed,
});

@override
State<UserAvatar> createState() => _UserAvatarState();
}

class _UserAvatarState extends State<UserAvatar> {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: widget.onPressed,
onTap: onPressed,
child: MouseRegion(
cursor: widget.onPressed != null
cursor: onPressed != null
? SystemMouseCursors.click
: SystemMouseCursors.basic,
child: SizedBox(
width: widget.size,
height: widget.size,
width: size,
height: size,
child: CircleAvatar(
radius: widget.size / 2,
radius: size / 2,
backgroundColor: colorDMBLight,
foregroundImage:
(widget.image != null) ? MemoryImage(widget.image!) : null,
foregroundImage: (image != null) ? MemoryImage(image!) : null,
child: Text(
(widget.username.characters.firstOrNull ?? "").toUpperCase(),
(username.characters.firstOrNull ?? "").toUpperCase(),
style: TextStyle(
color: Colors.white,
fontSize: 10 * widget.size / 24,
fontSize: 10 * size / 24,
fontWeight: FontWeight.bold,
),
),
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ generate-dart-files:
frb-generate $CARGO_TARGET_DIR=(justfile_directory() + "/target/frb_codegen"):
rm -f {{app_rust_base_dir}}/src/frb_*.rs
touch {{app_rust_base_dir}}/src/frb_generated.rs
rm -Rf {{app_dir}}/lib/core/*
rm -Rf {{app_dir}}/lib/core
mkdir {{app_dir}}/lib/core
cd {{app_dir}} && flutter_rust_bridge_codegen generate

Expand Down

0 comments on commit 5d8af9b

Please sign in to comment.