Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Class 颤振-抽屉作为子类未更新_Class_Dart_Flutter_Drawer - Fatal编程技术网

Class 颤振-抽屉作为子类未更新

Class 颤振-抽屉作为子类未更新,class,dart,flutter,drawer,Class,Dart,Flutter,Drawer,我是一个相当缺乏经验的程序员 我有一个抽屉,我已经创建了一个单独的类。我遇到的问题是抽屉的动态数据没有填充 我希望从共享首选项中检索的数据应该使用currUserL的值填充视图的第三行 它被正确评估,并将currUserL的值返回到控制台,但不会在抽屉中更新 我已经加载了一个about按钮(触发更新方法),当手动按下该按钮时,该按钮可以工作,但数据只有在抽屉保持打开状态时才会持久。当抽屉关闭时,它会恢复 抽屉式病人.省道 class DrawerPatient extends StatefulW

我是一个相当缺乏经验的程序员

我有一个抽屉,我已经创建了一个单独的类。我遇到的问题是抽屉的动态数据没有填充

我希望从共享首选项中检索的数据应该使用
currUserL
的值填充视图的第三行

它被正确评估,并将
currUserL
的值返回到控制台,但不会在抽屉中更新

我已经加载了一个about按钮(触发更新方法),当手动按下该按钮时,该按钮可以工作,但数据只有在抽屉保持打开状态时才会持久。当抽屉关闭时,它会恢复

抽屉式病人.省道

class DrawerPatient extends StatefulWidget {
  DrawerPatient({Key key}) : super(key: key);

  @override
  _DrawerPatientState createState() => new _DrawerPatientState();
}

class _DrawerPatientState extends State<DrawerPatient> {
  String currUserL = "nv3";

  Future getPref() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    currUserL = prefs.getString('currUserLast');

    debugPrint('user: $currUserL');
  }

  @override
  void initState() {
    getPref();
  }

  void update() {
    setState(() {
      getPref();
    });
  }
  @override
   Widget build(BuildContext context) {
     return Drawer(
        child: new ListView(
          children: <Widget>[
            new DrawerHeader(
              child: new Text('Patient Management'),
              ),
            new ListTile(
              title: new Text('search'),
              onTap: () {},
            ),
            new ListTile(
              title: new Text(currUserL),
              onTap: () {},
            ),
            new Divider(),
            new ListTile(
              title: new Text('About'),
              onTap: update,
            ),
          ],
        ));
     }
 }
class UserList extends StatefulWidget {
  UserList({Key key, this.title}) : super(key: key);

  final String title;
  final String titleHead = "User List";

  @override
  _UserListState createState() => new _UserListState();
  }

class _UserListState extends State<UserList> {
  : sortStr}, headers: {"Accept": "application/json"});

    setState(() {
      data = json.decode(response.body);
    });
    }

  @override
  void initState() {
    this.makeRequest();
//    DrawerPatient().createState().update();
  }


  void _refresh() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {

    return new Scaffold(
      appBar: new AppBar(

          title: new Text("Patient List"),

      drawer: new DrawerPatient(key: new UniqueKey()),
...
class DroperPatient扩展StatefulWidget{
抽屉病人({Key}):超级(Key:Key);
@凌驾
_DroperPatientState createState()=>新建_DroperPatientState();
}
类_DroperPatientState扩展状态{
字符串currUserL=“nv3”;
Future getPref()异步{
SharedReferences prefs=等待SharedReferences.getInstance();
currUserL=prefs.getString('currUserLast');
debugPrint('user:$currUserL');
}
@凌驾
void initState(){
getPref();
}
无效更新(){
设置状态(){
getPref();
});
}
@凌驾
小部件构建(构建上下文){
回程抽屉(
子:新列表视图(
儿童:[
新型抽屉阅读器(
子项:新文本(“患者管理”),
),
新ListTile(
标题:新文本(“搜索”),
onTap:(){},
),
新ListTile(
标题:新文本(currUserL),
onTap:(){},
),
新分隔符(),
新ListTile(
标题:新文本(“关于”),
onTap:更新,
),
],
));
}
}
userList.dart

class DrawerPatient extends StatefulWidget {
  DrawerPatient({Key key}) : super(key: key);

  @override
  _DrawerPatientState createState() => new _DrawerPatientState();
}

class _DrawerPatientState extends State<DrawerPatient> {
  String currUserL = "nv3";

  Future getPref() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    currUserL = prefs.getString('currUserLast');

    debugPrint('user: $currUserL');
  }

  @override
  void initState() {
    getPref();
  }

  void update() {
    setState(() {
      getPref();
    });
  }
  @override
   Widget build(BuildContext context) {
     return Drawer(
        child: new ListView(
          children: <Widget>[
            new DrawerHeader(
              child: new Text('Patient Management'),
              ),
            new ListTile(
              title: new Text('search'),
              onTap: () {},
            ),
            new ListTile(
              title: new Text(currUserL),
              onTap: () {},
            ),
            new Divider(),
            new ListTile(
              title: new Text('About'),
              onTap: update,
            ),
          ],
        ));
     }
 }
class UserList extends StatefulWidget {
  UserList({Key key, this.title}) : super(key: key);

  final String title;
  final String titleHead = "User List";

  @override
  _UserListState createState() => new _UserListState();
  }

class _UserListState extends State<UserList> {
  : sortStr}, headers: {"Accept": "application/json"});

    setState(() {
      data = json.decode(response.body);
    });
    }

  @override
  void initState() {
    this.makeRequest();
//    DrawerPatient().createState().update();
  }


  void _refresh() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {

    return new Scaffold(
      appBar: new AppBar(

          title: new Text("Patient List"),

      drawer: new DrawerPatient(key: new UniqueKey()),
...
class用户列表扩展StatefulWidget{
UserList({Key-Key,this.title}):super(Key:Key);
最后的字符串标题;
最终字符串titleHead=“用户列表”;
@凌驾
_UserListState createState()=>new_UserListState();
}
类_UserListState扩展状态{
:sortStr},头:{“Accept”:“application/json”});
设置状态(){
data=json.decode(response.body);
});
}
@凌驾
void initState(){
这是makeRequest();
//DroperPatient().createState().update();
}
void_refresh(){
setState((){});
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(“患者名单”),
抽屉:新抽屉患者(密钥:new UniqueKey()),
...
抽屉打开时

单击“关于(更新)”后的抽屉

你能试试这个吗

Future getCurrentUser() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();

  return prefs.getString('currUserLast');
}

void update() {
  val tempName = getCurrentUser();
  setState(() {
     currUserL = tempName;
  });
}

原因:基本上在调用
setState
之前等待
async
方法,所以我找到了答案,感谢@Dinesh为我指明了正确的方向

答案是将setState作为异步get prefs的依赖项

Future getPref() async {
SharedPreferences prefs = await SharedPreferences.getInstance();

setState(() {
  currUserI = prefs.getString('currUserId');
  currUserF = prefs.getString('currUserFirst');
  currUserL = prefs.getString('currUserLast');

  debugPrint('user: $currUserL');
});

}

谢谢,这不起作用,但它确实让我找到了正确的方向。发生了什么事?它应该起作用。我看到了你的另一个答案。但那只是一个代码移动。Future返回Future对象,我无法将其分配给String(currUserL。即使使用Future诱发。正在解析getCurrentUser()到字符串生成的未来对象。更新只是手动调用的,所以没有设置小部件加载时的状态。明白了。我没有编译。感谢您的响应