状态管理和数据加载中的问题

时间:2018-10-22 14:05:45

标签: json dart flutter

我试图在配置文件屏幕中加载用户数据,但是出现错误屏幕,提示该数据为空,但是当我用“ r”进行热加载时,我得到了正确的屏幕显示。我认为这与我加载数据或状态管理的方式有关。

_loadPage() async {
 setState(() {
  isLoading = true;
 });
 _sharedPreferences = await SharedPreferences.getInstance();
 _userData= _sharedPreferences.getString("user_details");
 currentUser = new User.fromJSON(json.decode(_userData));
 setState(() {
  isLoading = false;
 });
}

@override
void initState() {
 _loadPage();
 super.initState();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
 body: new CustomScrollView(
  shrinkWrap: true,
  slivers: <Widget>[
   new SliverPadding(
        padding: const EdgeInsets.all(32.0),
        sliver: new SliverList(
          delegate: new SliverChildListDelegate(
            <Widget>[

              new Column(
                children: <Widget>[

                  new Center(
                    child: new Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[

                        new CircleAvatar(
                           backgroundImage: currentUser?.profilUrl == null
                              ? new 
                           AssetImage("assets/icons/android/picture.jpg")
                              : new NetworkImage(
                              currentUser?.profilUrl),
                           maxRadius: 70.0,
                          minRadius: 50.0,
                        ),
                        new Text(
                          currentUser.name.toString(),
                        ),
                        new Text(
                          currentUser.email,
                        ),
                     ),
                  ),
                 ),
                ],
               ),
              ),

  ],
 ),
);

我得到了那些值,例如图片,文本和电子邮件。但是当我在终端中运行应用程序时,它显示此错误。

I/flutter (29402): ══╡ EXCEPTION CAUGHT BY GESTURE 
 ╞═══════════════════════════════════════════════════════════════════
I/flutter (29402): The following NoSuchMethodError was thrown while handling 
a gesture:
I/flutter (29402): The getter 'content' was called on null.
I/flutter (29402): Receiver: null
I/flutter (29402): Tried calling: content

1 个答案:

答案 0 :(得分:0)

json是异步的,当isLoading为true时使用await和CircularProgressIndicator。