refactor: minor profile loader improvement

This commit is contained in:
2025-05-28 11:40:53 +01:00
parent c6b76bc64d
commit 2ec17c6c41
4 changed files with 157 additions and 145 deletions

View File

@ -5,6 +5,9 @@ import 'package:ndk/shared/nips/nip19/nip19.dart';
import 'package:zap_stream_flutter/const.dart';
import 'package:zap_stream_flutter/widgets/avatar.dart';
// create simple sync cache to avoid extra re-draw
final Map<String, Metadata> syncProfileCache = {};
class ProfileLoaderWidget extends StatelessWidget {
final String pubkey;
final AsyncWidgetBuilder<Metadata?> builder;
@ -14,8 +17,18 @@ class ProfileLoaderWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
key: super.key ?? Key("profile-loader:$pubkey"),
future: ndk.metadata.loadMetadata(pubkey),
key: super.key,
initialData:
syncProfileCache.containsKey(pubkey)
? syncProfileCache[pubkey]
: null,
future: () async {
final profile = await ndk.metadata.loadMetadata(pubkey);
if (profile != null) {
syncProfileCache[pubkey] = profile;
}
return profile;
}(),
builder: builder,
);
}