flutter - 'RenderObject' 类型的值不能分配给 'RenderBox' 类型的变量

标签 flutter dart

 void getPositions() {
  final RenderBox renderBox = scheduleForDayKey.currentContext!.findRenderObject();
  final position = renderBox.localToGlobal(Offset.zero);
  print("POSITION of Red: $position ");
}
这部分用红色下划线:
scheduleForDayKey.currentContext!.findRenderObject()
当我悬停时,它告诉我错误:

A value of type 'RenderObject' can't be assigned to a variable of type 'RenderBox'.


可能这段代码已经过时了,或者是因为我在互联网上看到了很多类似的代码示例,出于某种原因对我不起作用。

最佳答案

试着写:

final RenderBox renderBox = scheduleForDayKey.currentContext!
                                             .findRenderObject() as RenderBox;

关于flutter - 'RenderObject' 类型的值不能分配给 'RenderBox' 类型的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67550315/

相关文章:

Flutter - 预期 : should do the following in order: emits an event Empty, NumberTriviaError,实际:空:不是 Stream 或 SteamQue

dart - Dart 中的 python 字典相当于什么?

flutter - flutter 的文件播放列表

dart - flutter : Is it possible to create multiple elements in a expansion tile?

android - 请创建文件,然后再次运行此命令

reflection - 在 Dart 中动态获取对象的字段

android - Flutter 中的缓冲文件读取

dart - 如何使用现有的应用程序 keystore 获取签名的 apk?

Flutter BoxShadow 重叠的 sibling

dart - 如何在 Flutter 的水平 ListView 中修复裁剪阴影