当我运行附加的颤振代码时,出现以下异常:
我应该对代码做哪些修改以纠正异常?
附言
当我改变 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Footstep : MonoBehaviour {
public AudioClip[] footstepClips;
public AudioSource audioSource;
public float footstepThreshold;
public float footstepRate;
private float lastFootstepTime;
public CharacterController controller;
void Update ()
{
if(controller.velocity.magnitude > footstepThreshold)
{
if(Time.time - lastFootstepTime > footstepRate)
{
lastFootstepTime = Time.time;
audioSource.PlayOneShot(footstepClips[Random.Range(0, footstepClips.Length)]);
}
}
}
}
类下的 size
方法中的 _maxColumns
和 _maxRows
变量时,getUnits()
变量应该改变。
Grid()
main.dart
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The getter 'width' was called on null.
Receiver: null
Tried calling: width
答案 0 :(得分:0)
您必须在 size
initState
变量
@override
void initState() {
super.initState();
size = Size(0.0, 0.0);
WidgetsBinding.instance.addPostFrameCallback(_afterLayout);
}
然后,在您的 _getSizes
方法中,在构建完所有内容后更改其内容
_getSizes() {
final RenderBox renderBox = _key.currentContext.findRenderObject();
setState(() {
size = renderBox.size;
print("SIZE of Box: $size");
});
}
编辑
如果您只是想在 SingleChildScrollView
小部件中使用 Column
,请将您的 SingleChildScrollView
包裹在 Flexible
中
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Flexible(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Container(
color: Colors.grey[200],
child: Grid(),
),
),
),
SizedBox(
height: 50,
),
Container(color: Colors.grey, height: 200),
],
),
不要忘记删除width
中的height
和Container
属性,而且size
变量不再需要了,所以你可以删除它