我想從會話變數中初始化_lastMapPosition的值,但在我的情況下,構建方法在Intistate方法之前運行。有什么簡單的方法可以在build widget方法之前初始化它嗎?我是flutter的新手,請提供完整的細節。我已經嘗試了futurebuilder,但我認為我的嘗試都是錯誤的。
。import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:permission/permission.dart';
import 'package:flutter_session/flutter_session.dart'。
class GmyApp extends StatefulWidget{
const GmyApp({Key key}) : super(key: key);
@override
_GmyApp createState() => _GmyApp()。
}
class _GmyApp extends State< GmyApp> {
var latitude_data;
var longitude_data;
Completer<GoogleMapController> _controller = Completer();
static const LatLng _center = const LatLng(10.8505, 76.2711)。
final Set<Marker> _markers = {};
LatLng _lastMapPosition= _center;
MapType _currentMapType = MapType.normal;
void _onMapTypeButtonPressed() {
setState(() {
_currentMapType = _currentMapType == MapType.normal
? MapType.satellite
: MapType.normal;
});
}
void _onAddMarkerButtonPressed() {
setState(() {
_markers.add(Marker(
//這個標記ID可以是任何能唯一識別每個標記的東西。
markerId: MarkerId(_lastMapPosition.toString())。
position: _lastMapPosition,
infoWindow: InfoWindow()
title: 'Your last location',
片段。'Location Refreshed every 1 hour',
),
圖示。BitmapDescriptor.defaultMarker,
));
});
}
void _onCameraMove(CameraPosition position) {
_lastMapPosition = position.target;
}
void _onMapCreated(GoogleMapController controller) { _controller.complete(controller).
_controller.complete(controller)。
}
@override
void initState() {
// TODO: implement initState
super.initState()。
setGps()。
}
Future setGps() async {
bool visible = false;
print('I am in GPS setter*')。
//var session = FlutterSession();
latitude_data = await FlutterSession().get('lat')。
longitude_data = await FlutterSession().get('longi') 。
print('User logi Id is in GPS* $longitude_data')。
print('User lat is in GPS* $latitude_data'/span>)。
LatLng center1=LatLng(latitude_data, longitude_data)。
print('Lat .lat is in GPS $center1'/span>)。
_lastMapPosition = center1;
print('Lat .lat lastMap Position is $_lastMapPosition')。
}
@override
Widget build(BuildContext context) {
return MaterialApp(
首頁。Scaffold(
appBar: AppBar(
標題。Text('Your Current Location is'),
backgroundColor: Colors.green[700],
),
身體。堆疊(
children: <Widget>[
GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition()
target: _lastMapPosition,
縮放。11.0,
),
mapType: _currentMapType,
markers: _markers,
onCameraMove: _onCameraMove,
),
填充(
padding: const EdgeInsets.all(16.0) 。
孩子。Align(
對齊。Alignment.topRight,
孩子。列(
children: <Widget> [
浮動按鈕(FloatingActionButton)
onPressed: _onMapTypeButtonPressed,
heroTag: "btn1"。
materialTapTargetSize: MaterialTapTargetSize.padded,
backgroundColor: Colors.green,
孩子。const Icon(Icons.map, size: 36.0) 。
),
SizedBox(高度:16.0)。
浮動按鈕(FloatingActionButton)
onPressed: _onAddMarkerButtonPressed,
heroTag: "btn2"。
materialTapTargetSize: MaterialTapTargetSize.padded,
backgroundColor: Colors.green,
孩子。const Icon(Icons.add_location, size: 36.0) 。
),
],
),
),
),
],
),
),
);
}
}
uj5u.com熱心網友回復:
因為你的資料是異步加載的,所以在你的資料被加載之前,構建將被呼叫。
有幾個解決方案,最好是使用一個狀態管理解決方案,然后檢查您的資料的當前狀態,以顯示或隱藏相關的小工具。
但是對于一個快速的解決方案,您可以使_lastMapPosition為空。
LatLng? _lastMapPosition;并且不給它一個初始值,然后在GoogleMap widget之前檢查資料是否被加載,如果沒有就顯示一個加載指示器
[
if(_lastMapPosition != null)
GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _lastMapPosition!,
zoom: 11.0,
),
...
),
if(_lastMapPosition == null)
Center(child: CircularProgressIndicator())。)
]
現在當你的資料被加載到setGps中時,使用setState來重建具有新資料的ui
setState((){
_lastMapPosition = center1;
});
我仍然建議使用狀態管理解決方案,它可以使你的代碼更具有可讀性和可預測性
。uj5u.com熱心網友回復:
只要在你的setGps()方法中加入setState方法即可
Future setGps() async {
bool visible = false;
print('I am in GPS setter*')。
//var session = FlutterSession();
latitude_data = await FlutterSession().get('lat')。
longitude_data = await FlutterSession().get('longi') 。
print('User logi Id is in GPS* $longitude_data')。
print('User lat is in GPS* $latitude_data'/span>)。
LatLng center1=LatLng(latitude_data, longitude_data)。
print('Lat .lat is in GPS $center1'/span>)。
_lastMapPosition = center1;
print('Lat .lat lastMap Position is $_lastMapPosition'/span>)。
setState(() {})。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/327611.html
標籤:
