Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

android - I have a question about using Shared_Preferences in Flutter

I have a question about using Shared_Preferences in Flutter.

My problem is that Shared_Preferences works fine in the Emulator,

but I don't even get a Shared_Preferences object when I actually connect my phone to the next line.

void initState() {
    // TODO: implement initState
    (() async {

      final SharedPreferences prefs = await SharedPreferences.getInstance();
      bool res = prefs.getBool('autoLoginKey');
      String id = prefs.getString('autoLoginId');
      String pw = prefs.getString('autoLoginPw');

     
          Navigator.push(
              context, // ?? ????, SecondRoute? ??
              MaterialPageRoute(
                  builder: (context) =>
                      MainPage()) // SecondRoute? ???? ??
          );
        }
      }
      else {
        Timer(Duration(seconds: 1), () {
          Navigator.pushReplacement(context, MaterialPageRoute(
              builder: (context) => LoginMainScreen()
          )
          );
        });
      }
        })();

    super.initState();
  }

[![enter image description here][1]][1] [1]: https://i.stack.imgur.com/nZZSC.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try wrapping that async function in this

WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  //Call your async function here
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...