【问题标题】:App crashing if already one 'await' is active如果已经有一个“等待”处于活动状态,则应用程序崩溃
【发布时间】:2020-08-14 19:11:14
【问题描述】:

我有一个显示世界时间的应用程序。我有一个用于更改世界各地不同位置的页面。这是一个 ListView。

Widget build(BuildContext context) {

    return Scaffold(
      backgroundColor: colorOne,
      appBar: AppBar(
        backgroundColor: Colors.black,
        title: Text("Change location"),
        centerTitle: true,
        elevation: 0.0,
      ),
      body: ListView.builder(
        physics: const AlwaysScrollableScrollPhysics(),
        padding: EdgeInsets.fromLTRB(5, 10, 5, 0),
        itemCount: locations.length,
        itemBuilder: (context, index) {
          return Card(
            child: ListTile(
              onTap: () {
                updateTime(index);
               ... rest code

如你所见,当我点击 ListTIle 时,它​​会调用 updateTime 函数

updateTime函数:

void updateTime(index) async {
    WorldTime instance = locations[index];
    await instance.getTime();
    Navigator.pop(context, {
      "location": instance.location,
      "flag": instance.flag,
      "time": instance.time,
      "date": instance.date,
      "isDayTime": instance.isDayTime,
    });
    // obtain shared preferences
    final savingLastLocation = await SharedPreferences.getInstance();

    // set value
    savingLastLocation.setString("location", instance.location);
    savingLastLocation.setString("url", instance.url);
    savingLastLocation.setString("flag", instance.flag);
}

如果用户在等待该功能时开始在图块上发送垃圾邮件,应用将显示完整的空白灰色屏幕或显示“布尔表达式必须为空”的红色死屏。

如果已经调用过一次,如何添加某种加载屏幕/小部件或防止再次调用函数?

【问题讨论】:

    标签: android ios flutter


    【解决方案1】:

    您可以使用忽略任何点击的 IgnorePointer 包裹屏幕。

    创建布尔变量。

    bool ignore = false;
    bool methodcalled = false; // new added line variable
    

    现在用 IgnorePointer 包裹你的脚手架。

    return IgnorePointer(
          ignoring: ignore,
          child: Scaffold(
    

    现在,当用户点击任何项目时,将忽略变量设置为 true。

    onTap: () {
            setState(() {
                      ignore = true;
             });
            updateTime(index).then((_){
                 setState(() {
                      ignore = false;
                 });
            });
           .... rest code
    

    在您的方法中添加 return。

     return 1
    
    
     void updateTime(index) async {
     if(!methodcalled){
        methodcalled = !methodcalled;
     }else{
        return 0;
     }
    
    WorldTime instance = locations[index];
    await instance.getTime();
    Navigator.pop(context, {
      "location": instance.location,
      "flag": instance.flag,
      "time": instance.time,
      "date": instance.date,
      "isDayTime": instance.isDayTime,
    });
    // obtain shared preferences
    final savingLastLocation = await SharedPreferences.getInstance();
    
    // set value
    savingLastLocation.setString("location", instance.location);
    savingLastLocation.setString("url", instance.url);
    savingLastLocation.setString("flag", instance.flag);
    methodcalled = !methodcalled;  // added line 
    return 1; // added line
    }
    

    【讨论】:

    • 我不能在 updateTime(index) 上使用 .then()。它说:“这里的表达有一种'void',因此不能使用。”如果我从我的函数中删除“void”,我会在调试控制台中得到这个异常“类型'()=> Null'不是'f'类型'(动态)=> FutureOr'的子类型”。如果我再次开始在 Tile 上发送垃圾邮件,应用程序会再次崩溃并显示黑屏
    • 只需在函数中添加 return 关键字,然后制作如下方法。然后((_){。我也更新了我的答案。@v4yne1
    • 谢谢!异常现在消失了,但是如果我快速双击,应用程序会显示全黑屏,我必须重新启动它
    【解决方案2】:

    onPressed 设置成这样

    onPressed: () async {
                    dynamic result =
                        await Navigator.pushNamed(context, '/location');
                    if (result != null) {
                      setState(() {
                        data = {
                          'location': result['location'],
                          'flag': result['flag'],
                          'time': result['time'],
                          'isDateTime': result['isDateTime']
                        };
                      });
                    }
                  },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多