【发布时间】:2021-08-09 22:02:38
【问题描述】:
这是我的小部件构建。
@override
Widget build(BuildContext context) {
final providerData = Provider.of<SpeedometerProvider>(context);
providerData.getSpeedUpdates();
这是我的提供者
class SpeedometerProvider with ChangeNotifier {
Speedometer _speedometer =
new Speedometer(currentSpeed: 0);
Speedometer get speedometer => _speedometer;
Stopwatch _stopwatch = Stopwatch();
final Geolocator _geolocator = Geolocator();
....
这是我的屏幕
SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.only(
right: 200.0, top: 550.0),
child: Center(
child: Column(children: <Widget>[
Text("Anlık HIZ",
style: Theme
.of(context)
.textTheme
.bodyText1),
SevenSegmentDisplay(
value:
'${providerData.speedometer.currentSpeed
.toStringAsFixed(0)}',
size: 4,
backgroundColor: Colors.white,
segmentStyle: HexSegmentStyle(
enabledColor: Colors.green,
disabledColor: Colors.white)),
Text("Kmh", style: Theme
.of(context)
.textTheme
.bodyText1)
])),
这是错误: 错误:在此 MapView Widget 上方找不到正确的 Provider
这可能是因为您使用了不包含提供程序的BuildContext
你的选择。有几种常见的情况:
-
您尝试读取的提供程序位于不同的路径中。
提供者是“范围的”。因此,如果您在路线内插入提供者,那么 其他路由将无法访问该提供程序。
-
您使用了
BuildContext,它是您尝试读取的提供程序的祖先。
【问题讨论】:
标签: flutter flutter-provider flutter-widget