手势捕获的异常:堆栈溢出

Posted

技术标签:

【中文标题】手势捕获的异常:堆栈溢出【英文标题】:Exception caught by gesture: Stack Overflow 【发布时间】:2020-11-01 11:58:40 【问题描述】:

每次我点击 ListView Card 时,此错误都会显示“”,如此屏幕截图所示 (https://www.screencast.com/t/udA8NZZ1AqsD)

这里的问题出在 Gesturedetector 的 onTap 代码中

截图列表 默认导航 PageRoute -> https://www.screencast.com/t/qKZQgaraFi 只需一个名为“测试”的纯文本 -> https://www.screencast.com/t/XOxGkANLZEjC 略微更改了导航页面路径 -> https://www.screencast.com/t/1F82pyQtQ2v9 带有新附加代码的手势检测器 -> https://www.screencast.com/t/hRefONV3o 从 firebase 传递数据的代码 -> https://www.screencast.com/t/24gc9zqfxmGX

结果链接: 结果 #1 -> https://www.screencast.com/t/PmiC5U6S2L 结果 #2 -> https://www.screencast.com/t/udA8NZZ1AqsD

我尝试了以下方法

尝试 1 尝试了默认的导航页面路由,手势检测器工作正常。缺点是我只能显示文本(参见上面的链接,只有一个名为“test”的纯文本),并且无法使用提供程序包颤振传递数据,如此屏幕截图所示

默认导航页面路由

GestureDetector(
  behavior: HitTestBehavior.translucent,
  onTap: () 
    Navigator.push(context, MaterialPageRoute(builder: (context) => ProductDetail()));
  ,
)

只有一个名为“测试”的纯文本

Text(
 'test',
 style: TextStyle(
    fontSize: 35.0,
    color: Colors.black45,
    fontWeight: FontWeight.w700
  ),
),

结果:见结果 #1

尝试 2 尝试了稍微更改的导航页面路由(请参阅上面的链接“稍微更改的导航页面路由”)以及普通文本(请参阅上面的链接 = 只是一个名为“测试”的纯文本)

导航页面路由

GestureDetector(
  behavior: HitTestBehavior.translucent,
  onTap: () 
    Navigator.of(context).push(
      MaterialPageRoute (builder: (BuildContext context) 
        return ProductDetail();
    )
  );
,

只有一个名为“测试”的纯文本

Text(
  'test',
  style: TextStyle(
    fontSize: 35.0,
    color: Colors.black45,
    fontWeight: FontWeight.w700
  ),
),

结果:见结果 #1

尝试 3 尝试使用新的附加代码(参见上面的链接“带有新附加代码的手势检测器”)以及普通文本(参见上面的链接,只是一个名为“test”的纯文本)

导航页面路由

GestureDetector(
  behavior: HitTestBehavior.translucent,
  onTap: () 
    itemNotifier.currentProditem = itemNotifier.itemList[index];
      Navigator.of(context).push(
        MaterialPageRoute (builder: (BuildContext context) 
          return ProductDetail();
      )
    );
  ,
),

只有一个名为“测试”的纯文本

Text(
  'test',
  style: TextStyle(
    fontSize: 35.0,
    color: Colors.black45,
    fontWeight: FontWeight.w700
  ),
),

结果:见结果 #2

尝试 4 尝试使用新的附加代码(请参阅“带有新附加代码的手势检测器”上方的链接)以及从 firebase 传递数据的代码(参见“从 firebase 传递数据的代码”上方的链接)

从 Firebase 传递数据的代码

Text(
  (itemNotifier.currentProditem.price),
  style: TextStyle(
    fontSize: 35.0,
    color: Colors.black45,
    fontWeight: FontWeight.w700
  ),
),

结果:见结果 #2

我的代码

Widget build(BuildContext context) 
    ItemNotifier itemNotifier = Provider.of<ItemNotifier>(context);
    return new Scaffold(
        backgroundColor: Color(0xffdcdcdc),
        appBar: new AppBar(
            centerTitle: true,
            title: new Text(
              'Discover Products',
              style: TextStyle(
                fontSize: 26.0,
                fontWeight: FontWeight.w600,
              ),
            ),
            backgroundColor: Color(0xFF352d5a)
        ),
        body: Padding(
          padding: EdgeInsets.only(top: 10.0, bottom: 10.0),
            child: ListView.builder(
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                physics: ClampingScrollPhysics(),
                itemCount: itemNotifier.itemList.length == null ? 0 : itemNotifier.itemList.length,
                itemBuilder: (BuildContext context, index) 
                  return GestureDetector(
                      behavior: HitTestBehavior.translucent,
                      onTap: () 
                       itemNotifier.currentProditem = itemNotifier.itemList[index];
                        Navigator.of(context).push(
                            MaterialPageRoute (builder: (BuildContext context) 
                              return ProductDetail();
                            )
                        );
                        // Navigator.push(context, MaterialPageRoute(builder: (context) => ProductDetail()));
                      ,
                      child: Stack(
                        children: <Widget>[
                          Container(
                            margin: EdgeInsets.fromLTRB(40.0, 5.0, 20.0, 5.0),
                            height: 140.0,
                            width: double.infinity,
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(20.0),
                            ),
                            child: Padding(
                              padding: EdgeInsets.fromLTRB(
                                  100.0, 10.0, 20.0, 20.0),
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Column(
                                    mainAxisAlignment: MainAxisAlignment
                                        .spaceBetween,
                                    crossAxisAlignment: CrossAxisAlignment
                                        .start,
                                    children: <Widget>[
                                      Row(
                                        mainAxisAlignment: MainAxisAlignment
                                            .spaceBetween,
                                        children: <Widget>[
                                          Text(
                                            (itemNotifier.itemList[index].price),
                                            style: TextStyle(
                                                fontSize: 24.0,
                                                color: Colors.black45,
                                                fontWeight: FontWeight.w700
                                            ),
                                          ),
                                          IconButton(
                                            icon: Icon(Icons.favorite),
                                            color: Colors.grey.shade400,
                                            onPressed: () 
                                              setState(() 
                                                print('Go to Favorites');
                                              );
                                            ,
                                          ),
                                        ],
                                      ),
                                       Container(
                                          width: 280.0,
                                          child: Text(
                                            (itemNotifier.itemList[index].producttitle),
                                            style: TextStyle(
                                                fontSize: 24.0,
                                                fontWeight: FontWeight.bold
                                            ),
                                            overflow: TextOverflow.ellipsis,
                                            maxLines: 1,
                                          ),
                                        ),
                                      SizedBox(
                                        height: 5.0,
                                      ),
                                    ],
                                  ),
                                  Container(
                                    width: 280.0,
                                    child: Text(
                                      (itemNotifier.itemList[index].description),
                                      style: TextStyle(
                                          color: Colors.grey
                                      ),
                                      overflow: TextOverflow.ellipsis,
                                      maxLines: 2,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                          Positioned(
                            left: 20.0,
                            top: 15.0,
                            bottom: 15.0,
                              child: Container(
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(20.0),
                                  boxShadow: [
                                    BoxShadow(
                                      color: Colors.black26,
                                      offset: Offset(0.0, 2.0),
                                      blurRadius: 6.0,
                                    ),
                                  ],
                                ),
                                child: ClipRRect(
                                  borderRadius: BorderRadius.circular(20.0),
                                  child: Image(
                                    width: 110.0,
                                    image: AssetImage(
                                        'image'
                                    ),
                                    fit: BoxFit.cover,
                                  ),
                                ),
                              ),
                          ),
                        ],
                      )
                  );
                
            ),
          ),
        );

  

【问题讨论】:

我很欣赏所有的细节,但不幸的是我迷路了......你肯定在某个地方有一个循环。看到堆栈跟踪...它指向itemNotifier.currentProditem = itemNotifier.itemList[index];...但不确定那里是如何生成循环的。 您可以为您的 ItemNotifier 添加代码吗? @Tanuj class ItemNotifier with ChangeNotifier List&lt;Proditem&gt; _itemList = []; Proditem _currentProditem; UnmodifiableListView&lt;Proditem&gt; get itemList =&gt; UnmodifiableListView(_itemList); Proditem get currentProditem =&gt; _currentProditem; set itemList(List&lt;Proditem&gt; itemList) _itemList = itemList; notifyListeners(); set currentProditem(Proditem proditem) currentProditem = proditem; notifyListeners(); 或者你可以在这里查看截图screencast.com/t/JUjv8ZYxMOYr @ClaudioRedi 你可以在这里查看 itemNotifier screencast.com/t/JUjv8ZYxMOYr 我可以知道你为什么要用这个更新itemNotifier.currentProditem = itemNotifier.itemList[index] 的原因吗? 【参考方案1】:

问题是您在 set 方法中再次递归调用 set 方法(因此堆栈溢出)

set currentProditem(Proditem proditem)
   currentProditem = proditem; //forgot the underscore,
   // it should be _currentProditem = proditem;
   // To change the variables not the setter again
   notifylisteners();

【讨论】:

天哪!有效!我完全忘记了 currentProditem 的下划线。非常感谢您的帮助。

以上是关于手势捕获的异常:堆栈溢出的主要内容,如果未能解决你的问题,请参考以下文章

了解 C 中的堆栈溢出处理

函数 堆栈溢出

Android SQLite NullPointer 异常和堆栈溢出异常 [重复]

如何在 Windows x64 C++ 应用程序中捕获堆栈溢出

如何在 x64 上制作“堆栈溢出”异常

为啥这个 BigInteger 值会导致堆栈溢出异常? C#