Back

[SOLVED] Realtime API and stream builder

  • 0
  • Flutter
Alan Gautham
8 Oct, 2023, 07:46

Hi guys,

I am trying to use realtime API with stream builder. It works but the issue is on the first load it shows nothing. When I change value from the backend it builds the ui.

`/// Account Stream

Stream<RealtimeMessage> userStream() { final realtime = Realtime(client); RealtimeSubscription subscription = realtime.subscribe(['account']); return subscription.stream; }`

`/// Account Screen

class AccountScreen extends StatefulWidget { const AccountScreen({super.key});

static const routeName = '/account-screen';

@override State<AccountScreen> createState() => _AccountScreenState(); }

class _AccountScreenState extends State<AccountScreen> { final seafarerStream = AppwriteAuthService.instance.userStream(); @override void initState() { seafarerStream; super.initState(); }

@override Widget build(BuildContext context) {

TypeScript
return Scaffold(
    appBar: const EzAppBar(
      title: 'Account',
      isPage: 'account',
    ),
    body: EzStream(
      stream: seafarerStream,
      onData: (data) {
        final doc = data.payload;
        return EzText(
          text: doc['name'].toString(),
          fontWeight: FontWeight.w700,
        );
      },
    ));

} } `

How to load data on first load with stream builder....?

Kindly assist. Thank you

TL;DR
Title: [SOLVED] Realtime API and stream builder - Initial data not loading Solution: To load data on the first load with stream builder, you need to fetch the user account details via `account.get()` to show initial data. Realtime events are only fired when the data on the observing channel changes and do not fetch on connect. Ensure that you get the document or data manually for the first time.
D5
8 Oct, 2023, 08:05

You will need to get the document or thing manually first time

D5
8 Oct, 2023, 08:05

As you would have done without realtime

darShan
8 Oct, 2023, 08:33

As @D5 said, you need to fetch the user account details via account.get() to show initial data.

Realtime event is only fired when a data on the observing channel changes. It doesn't do a fetch on connect.

Alan Gautham
8 Oct, 2023, 10:20

Thank you @darShan & @D5 Will try that. Was expecting to work like firebase

Alan Gautham
8 Oct, 2023, 10:21

[SOLVED] Realtime API and stream builder

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more