我在地图上有一系列标记,当点击其中一个标记时,显示小吃栏显示元素名称和'Ver'(显示)按钮。当我单击此按钮时,理论上可以转到其他页面。我有这个代码。
void onMapCreated(GoogleMapController controller) async {
setState(() {
mapController = controller;
markerMap = Map();
});
for (int i = 0; i < list.length; i++) {
var corte = list[0].keys.elementAt(i).split('/');
Marker marker = await mapController.addMarker(MarkerOptions(
position: LatLng(list[0].values.elementAt(i).latitude,
list[0].values.elementAt(i).longitude),
icon: BitmapDescriptor.defaultMarkerWithHue(
corte[1] == "tipo:0"
? BitmapDescriptor.hueRed
: corte[1] == "tipo:4"
? BitmapDescriptor.hueBlue
: corte[1] == "tipo:3"
? BitmapDescriptor.hueGreen
: corte[1] == "tipo:2"
? BitmapDescriptor.hueYellow
: null)));
markerMap[marker.id] = [corte,i];
}
mapController.onMarkerTapped.add((marker) {
scaffoldKey.currentState.removeCurrentSnackBar();
var objeto = markerMap[marker.id];
final snackBar = SnackBar(
duration: Duration(seconds: 10),
content: Text(objeto[0][0], textAlign: TextAlign.left),
backgroundColor: Colors.red[700],
action: SnackBarAction(
textColor: Colors.white,
label: 'Ver',
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => objeto[0][2] == "tipo:0"
? new MeterDetailsChartPage(
elemento: metersDetails[objeto[1]],
estate: this.estate,
parcela: widget.parcela)
: objeto[0][2] == "tipo:4"
? new ValvePage(
elemento: valvesDetails[objeto[1]],
estate: this.estate,
parcela: widget.parcela)
: objeto[0][2] == "tipo:3"
? new RelePage(
elemento: relesDetails[objeto[1]],
estate: this.estate,
parcela: widget.parcela)
: objeto[0][2] == "tipo:2"
? new SensorPage(
elemento: sensorsDetails[objeto[1]],
estate: this.estate,
parcela: widget.parcela)
: null));
},
));
scaffoldKey.currentState.showSnackBar(snackBar);
});
}
问题:当我单击“ Ver”时,返回此错误
I / flutter(11498):W小工具库引起的异常提示 ╞═════════════════════════════════════════════════ ══════════我/扑 (11498):在构建Builder(dirty)时引发了以下断言: I / flutter(11498):路线“ null”的构建器返回null。
我认为问题出在 async-await 和 builder:(context)。
有人有这个问题,知道如何解决吗?
-解决方案-
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => objeto[1] == "tipo:0"
? new MeterDetailsChartPage(
elemento: metersDetails.firstWhere((meters)=>meters.id==int.parse(objeto[2])),
estate: this.estate,
parcela: widget.parcela)
: objeto[1] == "tipo:4"
? new ValvePage(
elemento: valvesDetails.firstWhere((valves)=>valves.id==int.parse(objeto[2])),
estate: this.estate,
parcela: widget.parcela)
: objeto[1] == "tipo:3"
? new RelePage(
elemento: relesDetails.firstWhere((reles)=>reles.id==int.parse(objeto[2])),
estate: this.estate,
parcela: widget.parcela)
: objeto[1] == "tipo:2"
? new SensorPage(
elemento: sensorsDetails.firstWhere((sensors)=>sensors.id==int.parse(objeto[2])),
estate: this.estate,
parcela: widget.parcela)
:Navigator.pushNamed(context, EstatesPage.tag)));
答案 0 :(得分:0)
好吧,您要在构建器内部返回一个空窗口小部件,这是不允许的,因为框架告诉您。您可以稍微解决一下您的逻辑,以决定用户选择的路线。
var myNextRoute = objeto[0][2] == "tipo:0"
? new MeterDetailsChartPage(
elemento: metersDetails[objeto[1]],
estate: this.estate,
parcela: widget.parcela )
: objeto[0][2] == "tipo:4"
? new ValvePage(
elemento: valvesDetails[objeto[1]],
estate: this.estate,
parcela: widget.parcela )
: objeto[0][2] == "tipo:3"
? new RelePage(
elemento: relesDetails[objeto[1]],
estate: this.estate,
parcela: widget.parcela)
: objeto[0][2] == "tipo:2"
? new SensorPage(
elemento: sensorsDetails[objeto[1]],
estate: this.estate,
parcela: widget.parcela)
: null; //this null in your current logic is the problem
//because sometimes you will returning null in your build method. In this snippet we're avoiding this.
// avoiding pass a null widget to MaterialPageRoute.
if (myNextRoute != null)
Navigator.push( context,
new MaterialPageRoute(
builder: (context) => myNextRoute )
else {
// you can't go to a nextRoute
}