Check if an application is on its first run with Flutter

前端 未结 2 1852
滥情空心
滥情空心 2021-01-02 05:17

Basically, I want to have a screen/view that will open when the user opens up the app for the first time. This will be a login screen type of thing.

2条回答
  •  不知归路
    2021-01-02 05:57

    Use Shared Preferences Package. You can read it with FutureBuilder, and you can check if there is a bool named welcome for example. This is the implementation I have in my code:

    return new FutureBuilder(
          future: SharedPreferences.getInstance(),
          builder:
              (BuildContext context, AsyncSnapshot snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.none:
              case ConnectionState.waiting:
                return new LoadingScreen();
              default:
                if (!snapshot.hasError) {
                  @ToDo("Return a welcome screen")
                  return snapshot.data.getBool("welcome") != null
                      ? new MainView()
                      : new LoadingScreen();
                } else {
                  return new ErrorScreen(error: snapshot.error);
                }
            }
          },
        );
    

提交回复
热议问题