Search code examples
flutterdartsharedpreferencespreferencesshared

shared Preferences in flutter stop the app


I'm calling shared prefs in page I made as splash screen

  void initState() {
    super.initState();
    getPreferences().then((value){
      Timer(
        Duration(seconds: 2),
        (() => {
          Navigator.of(context).pushReplacement(new MaterialPageRoute(
              builder: (BuildContext context) => value))
        }),
      );
    });
  }

and here is my method

   getPreferences() async {
    WidgetsFlutterBinding.ensureInitialized();//??**
    SharedPreferences prefs = await SharedPreferences.getInstance();//the same with home screen in the skip button
    bool loadingScreen=prefs.getBool('seen');
    print("loadingScreen = $loadingScreen");
    StatefulWidget _screen= LoginScr();
     if (loadingScreen == null|| loadingScreen == false){
       _screen = Tut();
     }
     else if (loadingScreen == true){
       userName = prefs.getString('userName');
       if(userName ==null||userName=="null") {
         print(userName+' userName in main');
         _screen = LoginScr();
       } else {
         print(userName +'userName in main');
         _screen = HomePage();
       }
     }
     return _screen;
  }

By the way I don't know what is this line Widge,tsFlutterBinding.ensureInitialized();//??** and here is my log

Performing hot restart... Syncing files to device Android SDK built for x86... Restarted application in 1,776ms. I/flutter ( 8982): loadingScreen = true E/flutter ( 8982): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method '+' was called on null. E/flutter ( 8982): Receiver: null E/flutter ( 8982): Tried calling: +(" userName in main") E/flutter ( 8982): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) E/flutter ( 8982): #1
_SplashSCRState.getPreferences (package:plaster/UI/SplashSCR.dart:138:24) E/flutter ( 8982): E/flutter ( 8982): #2
_SplashSCRState.initState (package:plaster/UI/SplashSCR.dart:26:5) E/flutter ( 8982): #3 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4765:58) E/flutter ( 8982): #4 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5) E/flutter ( 8982): #5 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #6 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #7 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #8 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #9 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #10 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16) E/flutter ( 8982): #11 Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5) E/flutter ( 8982): #12 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5) E/flutter ( 8982): #13 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5) E/flutter ( 8982): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #16 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #17 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #18 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #19 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #22 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16) E/flutter ( 8982): #23 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11) E/flutter ( 8982): #24 Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5) E/flutter ( 8982): #25 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5) E/flutter ( 8982): #26 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4791:11) E/flutter ( 8982): #27 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5) E/flutter ( 8982): #28 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #29 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #30 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #31 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #32 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #33 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #34 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #35 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #36 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16) E/flutter ( 8982): #37 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11) E/flutter ( 8982): #38 Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5) E/flutter ( 8982): #39 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5) E/flutter ( 8982): #40 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4791:11) E/flutter ( 8982): #41 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5) E/flutter ( 8982): #42 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #43 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #44 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16) E/flutter ( 8982): #45 Element.r


Solution

  • You trying to print log:

    print(userName + ' userName in main');
    

    When userName is null:

    if (userName == null || userName == "null")
    

    And it raises exception. Modify print method usage ant try again. It should be like this:

    userName = prefs.getString('userName');
    if(userName == null || userName == "null") {
      print('first start, userName in main is null');
      _screen = LoginScr();
    } else {
      print('$userName is userName in main');
      _screen = HomePage();
    }