Back

Where do I put Async calls instead of initState?

  • 0
  • General
  • Flutter
ZachHandley
7 Jul, 2023, 23:59

As the title states, I have code I want to run at the beginning of my app loading and it's asynchronous and required for my HomeView, obviously I can't put it inside initState so where do I put it?

TL;DR
Put the asynchronous code inside a separate async function, let's say `setUpState()`. Then, call `setUpState()` from the `initState()` method. This way, you can run the asynchronous code at the beginning of app loading without violating the sync requirement of `initState()`.
Binyamin
8 Jul, 2023, 00:00

You put it inside a separate async function. And you call this function from the iniState method

ZachHandley
8 Jul, 2023, 00:00

oh

ZachHandley
8 Jul, 2023, 00:00

huh

ZachHandley
8 Jul, 2023, 00:00

that's confusing lol

Binyamin
8 Jul, 2023, 00:00

If you don't want the page the load before then you can create a middle splash page

Binyamin
8 Jul, 2023, 00:01

That's usually the way with stuff like this

ZachHandley
8 Jul, 2023, 00:01

lol

ZachHandley
8 Jul, 2023, 00:01

okay so

ZachHandley
8 Jul, 2023, 00:02
TypeScript
The following assertion was thrown building Builder:
_HomeViewState.initState() returned a Future.

State.initState() must be a void method without an `async` keyword.

Rather than awaiting on asynchronous work directly inside of initState, call a separate method to do this work without awaiting it.
ZachHandley
8 Jul, 2023, 00:02
TypeScript
Future<void> setupState() async {
    model.User? account = await ref.read(authAPIProvider).currentUserAccount();
    if (account != null) {
      model.Document userDocument =
          await ref.read(userAPIProvider).getUserData(account.$id);
      UserModel userModel = UserModel.fromMap(userDocument.data);
      needsLogin = userModel.sessionData.isNotEmpty;
      print("needsLogin: $needsLogin");
      if (!needsLogin) {
        final userData =
            await ref.read(dataAPIProvider).getUserData();
        userData.fold(
          (l) {
            print("Error getting data: ${l.message}");
          },
          (r) {
            setState(() {
              userData = r;
            });
          },
        );
      }
    }
  }

  @override
  void initState() async {
    super.initState();
    await setupState();
  }
ZachHandley
8 Jul, 2023, 00:03

this doesn't work and gives me an error

ZachHandley
8 Jul, 2023, 00:04

do I need to call a function normally and have that function call the async function?

Binyamin
8 Jul, 2023, 00:10

InitState should remain sync

ZachHandley
8 Jul, 2023, 00:10

okay so I did

ZachHandley
8 Jul, 2023, 00:10
TypeScript
void doSetup() {
    setupState();
  }

  @override
  void initState() {
    super.initState();
    doSetup();
  }
ZachHandley
8 Jul, 2023, 00:10

and that seems to work lmao

Binyamin
8 Jul, 2023, 00:10

You just call the inner function. And you can use then if you need

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