Christian Giupponi
Christian Giupponi

Reputation: 7618

Flutter and Future<bool> can't compare with bool value

I have seen this and this questions that has a similar problem but none of the answers works for me and can't figured out why.

I need to check if in y SharedPreferences there is a bool value that should set the initialRoute value.

here the code:

import 'package:consegne_cernusco/pages/consegna-gratuita.dart';
import 'package:consegne_cernusco/pages/onboarding.dart';
import 'package:consegne_cernusco/pages/single_shop.dart';
import 'package:flutter/material.dart';
import 'package:consegne_cernusco/pages/shop_list.dart';
import 'package:shared_preferences/shared_preferences.dart';
import './pages/home.dart';

void main() => runApp(MyApp());

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

class _MyAppState extends State<MyApp> {
  Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
  // Future<bool> _shoudShowOnboardingPage;

  Future<bool> _shoudShowOnboardingPage() async {
    return _prefs.then((SharedPreferences p) {
      // p.remove('shoudShowOnboardingPage');
      return p.getBool('shoudShowOnboardingPage') ?? true;
    });
  }

  String showMainPage() {
    if (_shoudShowOnboardingPage().then((value) => value == true) == true) {
      return OnBoarding.routeName;
    }

    return HomePage.routeName;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Consegne Cernusco',
      theme: ThemeData(
        primaryColor: Color(0xFF009EE4),
        accentColor: Color(0xFF009FE3),
        scaffoldBackgroundColor: Color(0xFFF3F5F7),
      ),
      initialRoute: showMainPage(),
      routes: {
        HomePage.routeName: (ctx) => HomePage(),
        ShopListPage.routeName: (ctx) => ShopListPage(),
        SingleShopPage.routeName: (ctx) => SingleShopPage(),
        ConsegnaGratuita.routeName: (ctx) => ConsegnaGratuita(),
        OnBoarding.routeName: (ctx) => OnBoarding()
      },
    );
  }
}

The problem is that this line:

  if (_shoudShowOnboardingPage().then((value) => value == true) == true)

always returns false but can't understand how to manage it.

Any advice?

Upvotes: 0

Views: 1456

Answers (1)

Christian Giupponi
Christian Giupponi

Reputation: 7618

Here how I solved thanks to @pskink comments.

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SharedPreferences.getInstance()
      .then((p) => p.getBool('shoudShowOnboardingPage') ?? true)
      .then(
        (b) => runApp(
          MyApp(
            shouldSee: b,
          ),
        ),
      );
}

Because of the new param passed in MyApp I have change the class:

class MyApp extends StatefulWidget {
  final bool shouldSee;
  MyApp({Key key, this.shouldSee});

  @override
  _MyAppState createState() => _MyAppState();
}

Now to access shouldSee just use widget.shouldSee:

initialRoute: widget.shouldSee == true ? OnBoarding.routeName : HomePage.routeName,

Upvotes: 1

Related Questions