加载 sharedpreferences Flutter 时出现错误屏幕

Error screen while loading sharedpreferences Flutter

您好,我在 Flutter 应用程序上使用 SharedPreferences 时遇到问题。加载要在构建函数中公开的 SharedPreferences 时出现错误屏幕。目前一切都已加载,我可以毫无错误地使用我的应用程序。 我得到的错误代码:“在 null 上调用了方法 getStringList”。这是我的代码:

SharedPreferences sp;
List<String> toprun = ["181","103","90","0","0","0","0","0","0","0"];

@override
void initState(){

SharedPreferences.getInstance().then((SharedPreferences shared)
{
  sp = shared;
  sp?.setStringList('toprun', toprun);
});      //Setting Data in my sp 
}

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: const Text('Title'),
    backgroundColor: Colors.redAccent,
  ),
  body: Padding(
    padding: EdgeInsets.fromLTRB(30.0, 40.0, 30.0, 0.0),
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget> [
        DataTable(
          columns: [
            DataColumn(label: Text('Results')),
          ],
          rows:
          sp.getStringList('toprun') // Get data I previously saved in my sp
              .map(
            ((element) => DataRow(
              cells: <DataCell>[
                DataCell(Text(element)),
              ],
            )),
          )
              .toList()
        )
      ],
    )
  ),
  bottomNavigationBar: NavBarWidget(index: 0,),
);
}

只是一个未经验证的猜测,但我认为 sp.getStringList('toprun') returns 一个承诺..所以你必须等待(等待)它或使用 futureBuilder..正如我所说,它是猜测,但在未来调用 .map 可能会导致错误

编辑:我测试过,没有问题。正如@kadri-nadir 所说,问题是 initState 并不意味着 asny 东西..所以在调用 build-- 方法时 sp 仍然为 null...以下对我有用:

class _MyAppState2 extends State<MyApp2> {
  List<String> toprun = ["181","103","90","0","0","0","0","0","0","0"];

  @override
  void initState(){
    SharedPreferences.getInstance().then((SharedPreferences shared){
      shared.setStringList('toprun', toprun);
    });      //Setting Data in my sp
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Title'),
        backgroundColor: Colors.redAccent,
      ),
      body: Padding(
          padding: EdgeInsets.fromLTRB(30.0, 40.0, 30.0, 0.0),
          child:
          FutureBuilder<SharedPreferences>(future: SharedPreferences.getInstance(),
            builder: (BuildContext context, AsyncSnapshot<SharedPreferences> snapshot) {
              if (snapshot.hasData) {
                return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      DataTable(
                          columns: [
                            DataColumn(label: Text('Results')),
                          ],
                          rows:
                          snapshot.data.getStringList(
                              'toprun') // Get data I previously saved in my sp
                              .map(
                            ((element) =>
                                DataRow(
                                  cells: <DataCell>[
                                    DataCell(Text(element)),
                                  ],
                                )),
                          )
                              .toList()
                      )
                    ]);
              }
              return Text('No data');
            })
      ),
    );
  }
}