flutter The getter 'physicalSize' isn't defined for the type 'MediaQueryData'. 是什么原因
时间: 2023-12-10 12:03:36 浏览: 34
`The getter 'physicalSize' isn't defined for the type 'MediaQueryData'` 是因为在 Flutter 2.0 之前的版本,`MediaQueryData` 类并没有 `physicalSize` 属性。如果你的 Flutter 版本较旧,那么可以使用 `MediaQueryData.size` 和 `MediaQueryData.devicePixelRatio` 属性来计算物理尺寸。
示例代码如下:
```dart
final size = MediaQuery.of(context).size;
final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
final physicalWidth = size.width * devicePixelRatio;
final physicalHeight = size.height * devicePixelRatio;
final physicalSize = Size(physicalWidth, physicalHeight);
```
如果你的 Flutter 版本比较新,那么可以直接使用 `MediaQueryData.physicalSize` 来获取物理尺寸。需要注意的是,`physicalSize` 属性是在 Flutter 2.0 中引入的,如果你的 Flutter 版本低于 2.0,那么需要升级到较新的版本才能使用 `physicalSize` 属性。
相关问题
Error: The getter 'body1' isn't defined for the class 'TextTheme'
这个错误通常发生在Flutter中,是因为您调用了TextTheme的一个不存在的属性。在Flutter中,TextTheme是一个类,它有很多不同的属性,如bodyText1,bodyText2等。 如果您尝试调用一个不存在的属性,就会收到这个错误。
解决这个问题的方法是确保您正在调用TextTheme类中已经定义过的属性。例如,如果您想要使用TextTheme中的bodyText1属性,则可以这样写:
```
Text("Hello World", style: Theme.of(context).textTheme.bodyText1)
```
请注意,这里的`bodyText1`是TextTheme类中已经定义过的属性,而不是任意的属性名。
报错了:The named parameter 'toastBuilder' isn't defined.
`toastBuilder`是Fluttertoast的1.5.0版本及以上才支持的参数,如果你的版本比较低,就会出现该错误。为了解决这个问题,你可以升级Fluttertoast到最新版本,或者使用其他方式来自定义toast视图。
如果你想使用其他方式来自定义toast视图,可以考虑使用`Toast`组件,这是Flutter SDK中自带的一个组件,可以用来创建自定义的toast视图。下面是一个使用`Toast`组件创建自定义toast视图的例子:
```dart
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
void showToast(String message) {
FToast fToast = FToast();
fToast.init(context);
Widget toast = Container(
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
color: Colors.black87,
),
child: Text(
message,
style: TextStyle(
fontSize: 16.0,
color: Colors.white,
),
),
);
fToast.showToast(
child: toast,
gravity: ToastGravity.BOTTOM,
toastDuration: Duration(seconds: 2),
);
}
```
在上面的代码中,我们使用`FToast`类创建了一个toast视图,并在其中添加了一个包含文本的容器。然后,我们将该容器作为`child`参数传递给`showToast`方法,同时还指定了toast的位置和显示时间。这样,就可以创建一个自定义的toast视图了。