Skip to content

navigateAfterFuture called multiple times #55

Description

@Akiat

First of all thank you for your great package.

The issue
The function assigned to navigateAfterFuture is called multiple times (2 or 3 times in my case).

The code

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  MainSingleton mainSingleton = MainSingleton();

  @override
  Widget build(BuildContext context) {
    return SplashScreen(
        routeName: "/",
        navigateAfterFuture: loadFromFuture(),
        title: new Text(
          APP_NAME,
          style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 30.0, color: Colors.green, fontFamily: "Roboto"),
        ),
        image: Image(image: AssetImage('assets/images/logo.png')),
    );
  }

  Future<Widget> loadFromFuture() async {
    await Firebase.initializeApp();
    // To catch background messages
    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
    FirebaseMessaging.onMessage.listen(firebaseMessagingForegroundHandler);
    String token = await FirebaseMessaging.instance.getToken();
    print("FirebaseMessaging token: $token");

    // These func only call my API to retreive some data.
    bool authorsLoaded = await loadAuthors();
    bool translatorsLoaded = await loadTranslators();
    bool tagsLoaded = await loadTags();
    bool captionsLoaded = await loadCaptions();

    return buildHomePage(authorsLoaded && translatorsLoaded && tagsLoaded && captionsLoaded);
  }

  Widget buildHomePage(bool everythingLoaded) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: APP_NAME,
      theme: ThemeData(
        primarySwatch: Colors.green,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: (everythingLoaded) ? HomePage() : buildRetryPage(),
    );
  }

Widget buildRetryPage() {
    return Scaffold(
      appBar: AppBar(title: Text(APP_NAME)),
      body: Container(
        child: Column(
          children: <Widget>[
            Text('PROBLEM, PLEASE RETRY'),
            Padding(padding: EdgeInsets.only(bottom: 20.0)),
            GFButton(
                text: "RETRY",
                onPressed: () async {
                  Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (BuildContext context) => MyApp()));
                }),
          ],
        ),
      ),
    );
  }

Expected behavior
It should call my callback only one time, or I done something wrong.

Thanks for your help 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions