回答編集履歴

1

コメントを受けての回答追記

2023/06/09 04:53

投稿

ta.fu
ta.fu

スコア1676

test CHANGED
@@ -1,3 +1,93 @@
1
+ 提示されたコードの一部を以下の様に変更したらどうでしょう。
2
+
3
+ ルート探索は、サーチアイコンのタップ時に処理し、探索終了後Widgetの再更新をするようにしています。
4
+ awaitを使わず、thenで非同期処理の結果でpolylineを更新して、完了後再描画するといった感じです。
5
+
6
+ initState内でルート探索を非同期で行うと、ウィジェットビルド前に何か処理を行おうとして問題となったのかな?
7
+ Chromeで試したのですが、ChromeProxyServiceのエラーが出なかったので、以下の対応も、想像での対応になってしまいますが。
8
+
9
+ ```dart
10
+ void _onMapCreated(GoogleMapController controller) {
11
+ mapController = controller;
12
+ }
13
+
14
+ //経路の配列を受け取って経路の設定をするメソッド
15
+ void GetRoutes() {
16
+ PolylinePoints polylinePoints = PolylinePoints();
17
+ polylinePoints
18
+ .getRouteBetweenCoordinates(
19
+ API_KYE,
20
+ PointLatLng(start.latitude, start.longitude),
21
+ PointLatLng(destination.latitude, destination.longitude),
22
+ )
23
+ .then((result) {
24
+ List<LatLng> polylineCoordinates = [];
25
+ if (result.points.isNotEmpty) {
26
+ result.points.forEach((PointLatLng point) {
27
+ polylineCoordinates.add(LatLng(point.latitude, point.longitude));
28
+ });
29
+ setState(() {
30
+ polyline.add(Polyline(
31
+ polylineId: const PolylineId("Route"),
32
+ visible: true,
33
+ color: Colors.blue,
34
+ width: 5,
35
+ points: polylineCoordinates));
36
+ });
37
+ }
38
+ });
39
+ }
40
+
41
+ //マップの表示を切り替えるためのウィジェット
42
+ void CreateMap() {
43
+ isVisiblePolyline = !isVisiblePolyline;
44
+ GetRoutes();
45
+ }
46
+
47
+ bool isVisiblePolyline = false;
48
+
49
+ //身体の部分(読み込みするとこ)
50
+ @override
51
+ Widget build(BuildContext context) {
52
+ return MaterialApp(
53
+ theme: ThemeData(
54
+ useMaterial3: true,
55
+ colorSchemeSeed: Colors.green[700],
56
+ ),
57
+ home: Scaffold(
58
+ appBar: AppBar(
59
+ title: const Text('Maps Sample App'),
60
+ elevation: 2,
61
+ ),
62
+ body: Stack(
63
+ children: <Widget>[
64
+ GoogleMap(
65
+ onMapCreated: _onMapCreated,
66
+ initialCameraPosition: CameraPosition(
67
+ target: _center,
68
+ zoom: 18.0,
69
+ ),
70
+ polylines: isVisiblePolyline ? polyline : const <Polyline>{},
71
+ myLocationEnabled: true,
72
+ markers: isVisiblePolyline
73
+ ? {
74
+ Marker(
75
+ markerId: const MarkerId("origin"),
76
+ position: start),
77
+ Marker(
78
+ markerId: const MarkerId("destination"),
79
+ position: destination)
80
+ }
81
+ : {
82
+ const Marker(
83
+ markerId: (MarkerId('marker1')),
84
+ position:
85
+ LatLng(34.663694401896244, 135.5185330148645))
86
+ },
87
+ ),
88
+ ```
89
+
90
+ ---
1
91
  タップ後にエラーが出ると仮定してです。
2
92
  もしエラーが出た操作方法が違うのであれば、どういった状況でエラーが出たかも書いて下さい。
3
93
  例えば起動直後とかどうかとか。