Flutter GoogleMap Provider 上下文问题
Flutter GoogleMap Provider Context problem
我有一个基本的有状态应用程序,带有一个 GoogleMap 基础,它会在按下 FloatingActionButton 时打开一个 BottomSheet。这用作 change/filter 一些列表的设置选项卡,然后由地图使用。我的提供商已设置,值可以是来自 GoogleMap dart 文件和 BottomSheet dart 文件的 read/saved。
我的主要问题是如何在 Provider.of()
不在小部件树中时提供上下文。例如,GoogleMap 在完成加载后运行 onMapCreated: _onMapCreated,
。在该函数中,我想从 Provider.of() 字符串中提取一个值,它告诉我要使用哪个列表(然后填充标记)。
我想做的事情:
- onMapCreated() 从 Provider 中提取值(稍后在 DB sqflite 查询中使用)
- bottomsheet 需要回调
updateMarkers()
以某种方式提供正确的上下文
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
GoogleMapController mapController;
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
print(Provider.of<MyAppStateContainer>(context, listen:false).getSomeValue); <---- Doesn't work
...
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<MyAppStateContainer>(
create: (contextooo) => MyAppStateContainer(),
child: MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My Map'),
backgroundColor: Colors.green[700],
),
//Put in a stack widget so can layer other widgets on top of map widget
body: Stack(
children: <Widget>[
//Builder so we can get a CTX made, and then Provider.of() finds the Provider
Builder(
builder: (context) => GoogleMap(
mapType: Provider.of<MyAppStateContainer>(context).getMapType, <--- Works fine
markers: _markers,
onMapCreated: _onMapCreated,
...
}
提供商:
class MyAppStateContainer extends ChangeNotifier {
MyAppStateContainer();
MapType _mapType = MapType.terrain;
String _someValue;
MapType get getMapType => _mapType;
String get getSomeValue => _someValue;
}
我已经尝试了各种传回的组合 BuildContext context
但遗憾的是我总是收到关于 Widget Tree 的错误:
Unhandled Exception: Error: Could not find the correct Provider<MyAppStateContainer> above this MyApp Widget
问题是在 _onMapCreated
中,您正试图使用 MyApp
的上下文访问 Provider
,它比 [=11] 的小部件层次更高=]本身。
将您的 home
小部件(Scaffold 及其下方的所有内容)转换为单独的 StatefulWidget
并且一切都应该开始工作,因为您将使用新小部件的上下文,即比 Provider
.
更低的小部件层次结构
我有一个基本的有状态应用程序,带有一个 GoogleMap 基础,它会在按下 FloatingActionButton 时打开一个 BottomSheet。这用作 change/filter 一些列表的设置选项卡,然后由地图使用。我的提供商已设置,值可以是来自 GoogleMap dart 文件和 BottomSheet dart 文件的 read/saved。
我的主要问题是如何在 Provider.of()
不在小部件树中时提供上下文。例如,GoogleMap 在完成加载后运行 onMapCreated: _onMapCreated,
。在该函数中,我想从 Provider.of() 字符串中提取一个值,它告诉我要使用哪个列表(然后填充标记)。
我想做的事情:
- onMapCreated() 从 Provider 中提取值(稍后在 DB sqflite 查询中使用)
- bottomsheet 需要回调
updateMarkers()
以某种方式提供正确的上下文
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
GoogleMapController mapController;
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
print(Provider.of<MyAppStateContainer>(context, listen:false).getSomeValue); <---- Doesn't work
...
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<MyAppStateContainer>(
create: (contextooo) => MyAppStateContainer(),
child: MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My Map'),
backgroundColor: Colors.green[700],
),
//Put in a stack widget so can layer other widgets on top of map widget
body: Stack(
children: <Widget>[
//Builder so we can get a CTX made, and then Provider.of() finds the Provider
Builder(
builder: (context) => GoogleMap(
mapType: Provider.of<MyAppStateContainer>(context).getMapType, <--- Works fine
markers: _markers,
onMapCreated: _onMapCreated,
...
}
提供商:
class MyAppStateContainer extends ChangeNotifier {
MyAppStateContainer();
MapType _mapType = MapType.terrain;
String _someValue;
MapType get getMapType => _mapType;
String get getSomeValue => _someValue;
}
我已经尝试了各种传回的组合 BuildContext context
但遗憾的是我总是收到关于 Widget Tree 的错误:
Unhandled Exception: Error: Could not find the correct Provider<MyAppStateContainer> above this MyApp Widget
问题是在 _onMapCreated
中,您正试图使用 MyApp
的上下文访问 Provider
,它比 [=11] 的小部件层次更高=]本身。
将您的 home
小部件(Scaffold 及其下方的所有内容)转换为单独的 StatefulWidget
并且一切都应该开始工作,因为您将使用新小部件的上下文,即比 Provider
.