参数类型不匹配 - 使用 Mapbox API
Argument Type Mismatch -using Mapbox API
美好的一天,
I am currently learning flutter under the Udemy course and at the moment is implementing maps in the app. The current lecture is using Google Maps to add markers on it when user taps on the screen, however I use a different API (Mapbox) and try to implement the same output.
目前我在尝试编译代码时遇到的问题是:enter image description here
我的代码可以在这里找到:
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import 'package:native_func_app/models/place.dart';
import '../models/place.dart';
class MapScreen extends StatefulWidget {
//const MapScreen({ Key? key }) : super(key: key);
final PlaceLocation initialLocation;
final bool isSelecting;
MapScreen({
this.initialLocation =
const PlaceLocation(latitude: 37.421, longitude: -122.084),
this.isSelecting = false,
});
@override
_MapScreenState createState() => _MapScreenState();
}
class _MapScreenState extends State<MapScreen> {
LatLng _pickedLocation;
void _selectLocation(LatLng position) {
setState(() {
_pickedLocation = position;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Your Map'),
),
body: FlutterMap(
options: MapOptions(
center: LatLng(widget.initialLocation.latitude,
widget.initialLocation.longitude),
zoom: 16.0,
//onTap: widget.isSelecting ? _selectLocation: null,
onTap: widget.isSelecting ? _selectLocation : () {},
),
layers: [
TileLayerOptions(
urlTemplate:
"https://api.mapbox.com/styles/v1/dhe/ckwd8qofr09gd14oq5d8djor9/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoiZGhlIiwiYSI6ImNrdnFybGw4ZzI1cWgycm91MXBpcW9oN3kifQ.fzK2_BFZrGU_vMpwTuU2Kg",
/*subdomains: ['a', 'b', 'c'],
attributionBuilder: (_) {
return Text("© OpenStreetMap contributors");
},*/
additionalOptions: {
'accessToken':
'pk.eyJ1IjoiZGhlIiwiYSI6ImNrdnFybGw4ZzI1cWgycm91MXBpcW9oN3kifQ.fzK2_BFZrGU_vMpwTuU2Kg',
'id': 'mapbox.mapbox-streets-v8',
}),
MarkerLayerOptions(
markers: _pickedLocation == null
? []
: [
Marker(
point: _pickedLocation,
builder: (ctx) => Container(
child: FlutterLogo(),
),
),
],
),
],
),
);
}
}
现在,有人要我用 (){} 替换空代码,看看错误是否消失了,它确实消失了。然而,当我重新编译时出现了一个新问题,如下所示:2nd issue crash at compile time
谁能帮我找出问题所在?
如果您需要访问我的项目 link 我正在使用的 Github 项目来查看源代码:
https://github.com/DheDeveloperUsv/native_func_app
非常欢迎任何解决此问题的帮助或见解。
将参数 tapPosition
添加到您的 _selectLocation
void _selectLocation(TapPosition tapPosition, LatLng position) {
setState(() {
_pickedLocation = position;
});
}
onTap 需要一个具有 2 个参数且没有 return 值 (void
) 的函数。
所以这不会起作用:
widget.isSelecting ? _selectLocation : () {}
_selecteLocation
是函数这1个参数
和(){}
没有参数。将 (){}
更改为 (TapPosition tapPosition, LatLng position){}
完整代码:
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import 'package:native_func_app/models/place.dart';
import '../models/place.dart';
class MapScreen extends StatefulWidget {
//const MapScreen({ Key? key }) : super(key: key);
final PlaceLocation initialLocation;
final bool isSelecting;
MapScreen({
this.initialLocation =
const PlaceLocation(latitude: 37.421, longitude: -122.084),
this.isSelecting = false,
});
@override
_MapScreenState createState() => _MapScreenState();
}
class _MapScreenState extends State<MapScreen> {
LatLng _pickedLocation;
void _selectLocation(TapPosition tapPosition, LatLng position) {
setState(() {
_pickedLocation = position;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Your Map'),
),
body: FlutterMap(
options: MapOptions(
center: LatLng(widget.initialLocation.latitude,
widget.initialLocation.longitude),
zoom: 16.0,
//onTap: widget.isSelecting ? _selectLocation: null,
onTap: widget.isSelecting ? _selectLocation : (TapPosition tapPosition, LatLng position) {},
),
layers: [
TileLayerOptions(
urlTemplate:
"https://api.mapbox.com/styles/v1/dhe/ckwd8qofr09gd14oq5d8djor9/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoiZGhlIiwiYSI6ImNrdnFybGw4ZzI1cWgycm91MXBpcW9oN3kifQ.fzK2_BFZrGU_vMpwTuU2Kg",
/*subdomains: ['a', 'b', 'c'],
attributionBuilder: (_) {
return Text("© OpenStreetMap contributors");
},*/
additionalOptions: {
'accessToken':
'pk.eyJ1IjoiZGhlIiwiYSI6ImNrdnFybGw4ZzI1cWgycm91MXBpcW9oN3kifQ.fzK2_BFZrGU_vMpwTuU2Kg',
'id': 'mapbox.mapbox-streets-v8',
}),
MarkerLayerOptions(
markers: _pickedLocation == null
? []
: [
Marker(
point: _pickedLocation,
builder: (ctx) => Container(
child: FlutterLogo(),
),
),
],
),
],
),
);
}
}
美好的一天,
I am currently learning flutter under the Udemy course and at the moment is implementing maps in the app. The current lecture is using Google Maps to add markers on it when user taps on the screen, however I use a different API (Mapbox) and try to implement the same output.
目前我在尝试编译代码时遇到的问题是:enter image description here
我的代码可以在这里找到:
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import 'package:native_func_app/models/place.dart';
import '../models/place.dart';
class MapScreen extends StatefulWidget {
//const MapScreen({ Key? key }) : super(key: key);
final PlaceLocation initialLocation;
final bool isSelecting;
MapScreen({
this.initialLocation =
const PlaceLocation(latitude: 37.421, longitude: -122.084),
this.isSelecting = false,
});
@override
_MapScreenState createState() => _MapScreenState();
}
class _MapScreenState extends State<MapScreen> {
LatLng _pickedLocation;
void _selectLocation(LatLng position) {
setState(() {
_pickedLocation = position;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Your Map'),
),
body: FlutterMap(
options: MapOptions(
center: LatLng(widget.initialLocation.latitude,
widget.initialLocation.longitude),
zoom: 16.0,
//onTap: widget.isSelecting ? _selectLocation: null,
onTap: widget.isSelecting ? _selectLocation : () {},
),
layers: [
TileLayerOptions(
urlTemplate:
"https://api.mapbox.com/styles/v1/dhe/ckwd8qofr09gd14oq5d8djor9/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoiZGhlIiwiYSI6ImNrdnFybGw4ZzI1cWgycm91MXBpcW9oN3kifQ.fzK2_BFZrGU_vMpwTuU2Kg",
/*subdomains: ['a', 'b', 'c'],
attributionBuilder: (_) {
return Text("© OpenStreetMap contributors");
},*/
additionalOptions: {
'accessToken':
'pk.eyJ1IjoiZGhlIiwiYSI6ImNrdnFybGw4ZzI1cWgycm91MXBpcW9oN3kifQ.fzK2_BFZrGU_vMpwTuU2Kg',
'id': 'mapbox.mapbox-streets-v8',
}),
MarkerLayerOptions(
markers: _pickedLocation == null
? []
: [
Marker(
point: _pickedLocation,
builder: (ctx) => Container(
child: FlutterLogo(),
),
),
],
),
],
),
);
}
}
现在,有人要我用 (){} 替换空代码,看看错误是否消失了,它确实消失了。然而,当我重新编译时出现了一个新问题,如下所示:2nd issue crash at compile time
谁能帮我找出问题所在?
如果您需要访问我的项目 link 我正在使用的 Github 项目来查看源代码:
https://github.com/DheDeveloperUsv/native_func_app
非常欢迎任何解决此问题的帮助或见解。
将参数 tapPosition
添加到您的 _selectLocation
void _selectLocation(TapPosition tapPosition, LatLng position) {
setState(() {
_pickedLocation = position;
});
}
onTap 需要一个具有 2 个参数且没有 return 值 (void
) 的函数。
所以这不会起作用:
widget.isSelecting ? _selectLocation : () {}
_selecteLocation
是函数这1个参数
和(){}
没有参数。将 (){}
更改为 (TapPosition tapPosition, LatLng position){}
完整代码:
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import 'package:native_func_app/models/place.dart';
import '../models/place.dart';
class MapScreen extends StatefulWidget {
//const MapScreen({ Key? key }) : super(key: key);
final PlaceLocation initialLocation;
final bool isSelecting;
MapScreen({
this.initialLocation =
const PlaceLocation(latitude: 37.421, longitude: -122.084),
this.isSelecting = false,
});
@override
_MapScreenState createState() => _MapScreenState();
}
class _MapScreenState extends State<MapScreen> {
LatLng _pickedLocation;
void _selectLocation(TapPosition tapPosition, LatLng position) {
setState(() {
_pickedLocation = position;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Your Map'),
),
body: FlutterMap(
options: MapOptions(
center: LatLng(widget.initialLocation.latitude,
widget.initialLocation.longitude),
zoom: 16.0,
//onTap: widget.isSelecting ? _selectLocation: null,
onTap: widget.isSelecting ? _selectLocation : (TapPosition tapPosition, LatLng position) {},
),
layers: [
TileLayerOptions(
urlTemplate:
"https://api.mapbox.com/styles/v1/dhe/ckwd8qofr09gd14oq5d8djor9/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoiZGhlIiwiYSI6ImNrdnFybGw4ZzI1cWgycm91MXBpcW9oN3kifQ.fzK2_BFZrGU_vMpwTuU2Kg",
/*subdomains: ['a', 'b', 'c'],
attributionBuilder: (_) {
return Text("© OpenStreetMap contributors");
},*/
additionalOptions: {
'accessToken':
'pk.eyJ1IjoiZGhlIiwiYSI6ImNrdnFybGw4ZzI1cWgycm91MXBpcW9oN3kifQ.fzK2_BFZrGU_vMpwTuU2Kg',
'id': 'mapbox.mapbox-streets-v8',
}),
MarkerLayerOptions(
markers: _pickedLocation == null
? []
: [
Marker(
point: _pickedLocation,
builder: (ctx) => Container(
child: FlutterLogo(),
),
),
],
),
],
),
);
}
}