flutter - “Null”类型不是“List”类型的子类型'函数结果'错误
问题描述
我面临这样的错误,我该怎么办?
由于我找不到错误的来源,我在下面分享了必要的代码页,我正面临一个新错误,https://prnt.sc/1s604iu。
..................................................... .....................
-主要的-
import 'dart:async';
import 'package:final_prayer/maps_with_mosque/blocs/application_bloc.dart';
import 'package:final_prayer/maps_with_mosque/models/place.dart';
import 'package:final_prayer/praying_Screen/praying_ScreenPage.dart';
import 'package:final_prayer/qiblah/qiblah_Screen.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:provider/provider.dart';
class mosque_App extends StatefulWidget {
const mosque_App({Key? key}) : super(key: key);
@override
_mosque_AppState createState() => _mosque_AppState();
}
class _mosque_AppState extends State<mosque_App> {
Completer<GoogleMapController> _mapController = Completer();
late StreamSubscription locationSubscription;
late StreamSubscription boundsSubscription;
final _locationController = TextEditingController();
Future<Applicationbloc?> verileriCagir()async{
final applicationBloc= await Provider.of<Applicationbloc>(context,listen: false);
}
Future<dynamic?> callBounds()async{
final applicationBloc= await Provider.of<Applicationbloc>(context,listen: false);
}
@override
void initState() {
var applicationBloc;
verileriCagir().whenComplete((){
locationSubscription= applicationBloc.selectedLocation.stream.listen((place) {
if(place!=null){
_goToPlace(place);
}else{
_locationController.text = "";
}
}
);
});
boundsSubscription = applicationBloc.bounds.stream.listen((bounds) async{
final GoogleMapController controller = await _mapController.future;
controller.animateCamera(CameraUpdate.newLatLngBounds(bounds, 50.0));
});
super.initState();
}
@override
void dispose() {
final applicationBloc= Provider.of<Applicationbloc>(context,listen: false);
applicationBloc.dispose();
_locationController.dispose();
boundsSubscription.cancel();
locationSubscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
final applicationBloc= Provider.of<Applicationbloc>(context);
return Scaffold(
body:(applicationBloc.currentLocation == null)
? Center(child: CircularProgressIndicator(),)
:Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children:<Widget> [
TextField(
decoration:InputDecoration(
hintText: 'Konum Ara', //Search Location
suffixIcon: Icon(Icons.search),
),
onChanged: (value)=>applicationBloc.searchPlaces(value),
),
Stack(
children: [
Container(
height: 500.0,
child: GoogleMap(
mapType: MapType.normal,
myLocationEnabled: true,
initialCameraPosition: CameraPosition(
target: LatLng(
applicationBloc.currentLocation!.latitude,
applicationBloc.currentLocation!.longitude),
zoom: 14,
),
onMapCreated: (GoogleMapController controller) {
_mapController.complete(controller);
},
markers: Set<Marker>.of(applicationBloc.markers!),
),
),
if(applicationBloc.searchResults != null && applicationBloc.searchResults!.length != 0)
Container(
height: 300.0,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.black.withOpacity(.6),
backgroundBlendMode: BlendMode.darken
),
),
if(applicationBloc.searchResults!= null)
Container(
height: 300.0,
child: ListView.builder( itemCount: applicationBloc.searchResults!.length,
itemBuilder: (context,index){
return ListTile(
title:Text(applicationBloc.searchResults![index].description, style: TextStyle(color: Colors.white),),
onTap: (){
applicationBloc.setSelectedLocation(
applicationBloc.searchResults![index].placeID
);
},
);
},
),
),
],
),
SizedBox(
height: 30,
),
Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Wrap(
spacing: 8.0,
children: [
FilterChip(label: Text('En Yakın Camiyi Bul'),
onSelected: (value)=>applicationBloc.togglePlaceType('mosque', value),
selected: applicationBloc.placeType == 'mosque',
selectedColor: Colors.blue,
)
],
),
),
),
],
),
),
bottomNavigationBar:Container(
decoration:BoxDecoration(
color: Colors.blueGrey.shade900,
borderRadius: BorderRadius.vertical(
top: Radius.circular(36.0),
),
),
padding: const EdgeInsets.all(1),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children:<Widget> [
IconButton(
icon:Image.asset('assets/main_screen/icons/prayer_icon.png'),
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context)=>PrayPage()));
},
),
IconButton(
icon:Image.asset('assets/main_screen/icons/quran_icon.png'),
onPressed: (){},
),
IconButton(
icon:Image.asset('assets/main_screen/icons/calendar_icon.png'),
onPressed: (){},
),
IconButton(
icon:Image.asset('assets/main_screen/icons/mosque_icon.png'),
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context)=>mosque_App()));
},
),
IconButton(
icon:Image.asset('assets/main_screen/icons/compass_icon.png'),
onPressed: (){
Navigator.push(context,MaterialPageRoute(builder: (context)=> QiblahCompass()));
},
),
],
),
) ,
);
}
Future<void> _goToPlace(Place place)async{
final GoogleMapController controller = await _mapController.future;
controller.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
target: LatLng(
place.geometry.location.lat,
place.geometry.location.lng),
zoom: 14.0),
)
);
}
}
-标记服务-
import 'package:final_prayer/maps_with_mosque/models/place.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class MarkerService{
LatLngBounds? bounds(Set<Marker>? markers){
if(markers == null || markers.isEmpty) return null;
return createBounds(markers.map((m) => m.position).toList());
}
LatLngBounds createBounds(List<LatLng> positions) {
final southwestLat = positions.map((p) => p.latitude).reduce((value, element) => value < element ? value : element); // smallest
final southwestLon = positions.map((p) => p.longitude).reduce((value, element) => value < element ? value : element);
final northeastLat = positions.map((p) => p.latitude).reduce((value, element) => value > element ? value : element); // biggest
final northeastLon = positions.map((p) => p.longitude).reduce((value, element) => value > element ? value : element);
return LatLngBounds(
southwest: LatLng(southwestLat, southwestLon),
northeast: LatLng(northeastLat, northeastLon)
);
}
Marker? createMarkerFromPlace(Place place,bool center){
var markerId= place.name;
if (center) markerId = 'center';
return Marker(
markerId: MarkerId(markerId),
draggable: false,
visible: (center) ? false : true,
infoWindow: InfoWindow(
title: place.name, snippet: place.vicinity),
position: LatLng(place.geometry.location.lat,
place.geometry.location.lng)
);
}
}
-应用程序块-
import 'dart:async';
import 'package:final_prayer/maps_with_mosque/models/geometry.dart';
import 'package:final_prayer/maps_with_mosque/models/location.dart' ;
import 'package:final_prayer/maps_with_mosque/models/place.dart';
import 'package:final_prayer/maps_with_mosque/models/place_search.dart';
import 'package:final_prayer/maps_with_mosque/services/geolocator_service.dart';
import 'package:final_prayer/maps_with_mosque/services/marker_service.dart';
import 'package:flutter/cupertino.dart';
import 'package:geolocator/geolocator.dart';
import 'package:final_prayer/maps_with_mosque/services/places_service.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class Applicationbloc with ChangeNotifier{
final geoLocatorService= GeolocatorService();
final placesService = PlacesService();
final markerService = MarkerService();
//Variables-Verilerim//
Position? currentLocation;
List<PlaceSearch>? searchResults;
StreamController<Place> selectedLocation= StreamController<Place>.broadcast();
StreamController<LatLngBounds> bounds = StreamController<LatLngBounds>.broadcast();
Place? selectedLocationStatic;
String? placeType;
late List<Marker>? markers;
Applicationbloc(){
setCurrentLocation();
}
setCurrentLocation()async{
currentLocation = await geoLocatorService.getCurrentLocation();
selectedLocationStatic=Place(name:'',geometry: Geometry(location:Location(lat:currentLocation!.latitude,lng: currentLocation!.longitude)), vicinity: '');
notifyListeners();
}
searchPlaces(String searchTerm) async {
searchResults = (await placesService.getAutocomplate(searchTerm)).cast<PlaceSearch>();
notifyListeners();
}
setSelectedLocation(String placeId) async {
var sLocation = await placesService.getPlace(placeId);
selectedLocation.add(sLocation);
selectedLocationStatic = sLocation;
searchResults = null;
notifyListeners();
}
clearSelectedLocation() {
selectedLocationStatic = null;
searchResults = null;
placeType = null;
notifyListeners();
}
togglePlaceType(String value,bool selected)async{
if(selected){
placeType=value;
}
else{
placeType=null;
}
if(placeType != null){
var places = await placesService.getPlaces(
selectedLocationStatic!.geometry.location.lat,
selectedLocationStatic!.geometry.location.lng,
placeType!
);
markers= [];
if (places.length > 0) {
var newMarker = markerService.createMarkerFromPlace(places[0],false);
markers!.add(newMarker!);
}
var locationMarker = markerService.createMarkerFromPlace(selectedLocationStatic!,true);
markers!.add(locationMarker!);
var _bounds = markerService.bounds(Set<Marker>.of(markers!));
bounds.add(_bounds!);
notifyListeners();
}
}
@override
void dispose() {
selectedLocation.close();
bounds.close();
super.dispose();
}
}
解决方案
删除!
消费列表中的市场。如果要使用!
运算符,则必须使用 null-check operator 指定该变量?
。
IElate List<Marker>? markers
推荐阅读
- go - Bcrypt 需要很多时间吗?
- python - 根据先前的值在数据框中创建下一行
- html - 高度取决于其他组件的高度 React CSS
- android - 在 Android Studio 北极狐中使用文件模板时如何定位绝对路径?
- php - 有没有办法获取在哪里匹配的列的值?
- android - Android检测相机是否拍摄图像
- java - List addAll(aList) then aList.clear(): 如果实现既不是 ArrayList 也不是 LinkedList,我会得到 null 吗?
- python - 将列表列表转换为数据框列
- sql - 基于另一个列值合并列值 - TSQL
- javascript - 使用键盘麦克风在(ipad/移动设备)上听写后保存用于填充文本框的语音