【问题标题】:Issues in state management and loading the data状态管理和加载数据的问题
【发布时间】:2018-10-22 14:05:44
【问题描述】:

我正在尝试在配置文件屏幕中加载用户数据,但我得到一个错误屏幕,显示数据为空,但是当我使用“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

【问题讨论】:

  • 您的标题和内容不匹配。在一个中,您谈论处理解析中的错误,而另一个您谈论状态管理和热重载。
  • 你在哪里得到 null
  • 从 currentUser.name 获取 null 但是当您进行热重载时它不会再次显示 null。
  • 请发布整个课程,因为这没有给我们足够的信息
  • 谢谢你,我更新了代码。

标签: json dart flutter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-11-23
    • 2020-04-10
    • 2016-02-28
    • 2021-04-07
    • 2020-05-15
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多