无法从 flutter 中的不同 class 访问提供者 class 中的变量
cant access variables in provider class from a different class in flutter
我有下面的 classes
class AppState with ChangeNotifier{
Set<Polyline> _polylines = Set<Polyline>();
LocationData _currentLocation;
Set<Polyline> get polylines => _polylines;
LocationData get currentLocation => _currentLocation;
}
在我的 main.dart 我有
void main() => runApp(
MultiProvider(
providers: [
ChangeNotifierProvider.value(value: AppState())
],
child: MyApp()),
);
然后我尝试在不同的地方访问它class
...
@override
Widget build(BuildContext context) {
final appState = Provider.of<AppState>(context);
appState.currentLocation != null?Container():Text()
}
...
But the problem is that I get the error that
The getter 'currentLocation' isn't defined for the type 'AppState'.
Try importing the library that defines 'currentLocation', correcting the name to the name of an existing getter, or defining a getter or field named 'currentLocation'.
似乎看不出哪里做错了。我该如何解决这个问题
您应该首先使用默认构造函数创建 ChangeNotifier
,如官方文档中所述。
Provider official documentation on Pub.dev
- 请在 create 中创建一个新的 ChangeNotifier。
ChangeNotifierProvider(
create: (_) => new MyChangeNotifier(),
child: ...
)
- 不要使用 ChangeNotifierProvider.value 创建您的 ChangeNotifier。
ChangeNotifierProvider.value(
value: new MyChangeNotifier(),
child: ...
)
- 不要根据随时间变化的变量创建 ChangeNotifier。
在这种情况下,您的 ChangeNotifier 永远不会在值更改时更新。
int count;
ChangeNotifierProvider(
create: (_) => new MyChangeNotifier(count),
child: ...
)
如果您想将变量传递给 ChangeNotifier,请考虑使用 ChangeNotifierProxyProvider。
所以你的 MultiProvider
应该看起来像这样:
void main() => runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => AppState())
],
child: MyApp()),
);
我有下面的 classes
class AppState with ChangeNotifier{
Set<Polyline> _polylines = Set<Polyline>();
LocationData _currentLocation;
Set<Polyline> get polylines => _polylines;
LocationData get currentLocation => _currentLocation;
}
在我的 main.dart 我有
void main() => runApp(
MultiProvider(
providers: [
ChangeNotifierProvider.value(value: AppState())
],
child: MyApp()),
);
然后我尝试在不同的地方访问它class
...
@override
Widget build(BuildContext context) {
final appState = Provider.of<AppState>(context);
appState.currentLocation != null?Container():Text()
}
...
But the problem is that I get the error that
The getter 'currentLocation' isn't defined for the type 'AppState'.
Try importing the library that defines 'currentLocation', correcting the name to the name of an existing getter, or defining a getter or field named 'currentLocation'.
似乎看不出哪里做错了。我该如何解决这个问题
您应该首先使用默认构造函数创建 ChangeNotifier
,如官方文档中所述。
Provider official documentation on Pub.dev
- 请在 create 中创建一个新的 ChangeNotifier。
ChangeNotifierProvider(
create: (_) => new MyChangeNotifier(),
child: ...
)
- 不要使用 ChangeNotifierProvider.value 创建您的 ChangeNotifier。
ChangeNotifierProvider.value(
value: new MyChangeNotifier(),
child: ...
)
- 不要根据随时间变化的变量创建 ChangeNotifier。
在这种情况下,您的 ChangeNotifier 永远不会在值更改时更新。
int count;
ChangeNotifierProvider(
create: (_) => new MyChangeNotifier(count),
child: ...
)
如果您想将变量传递给 ChangeNotifier,请考虑使用 ChangeNotifierProxyProvider。
所以你的 MultiProvider
应该看起来像这样:
void main() => runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => AppState())
],
child: MyApp()),
);