大佬教程收集整理的这篇文章主要介绍了获取“未处理的异常:NoSuchMethodError:方法'map'被调用为空”,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_869_3@main.dart 文件
import 'package:Flutter/material.dart';
import 'package:weather_app/ListModel.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:weather_app/model.dart';
voID main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@overrIDe
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var ListModel;
bool circular = true;
@overrIDe
voID initState() {
super.initState();
getData();
}
voID getData() async {
var res = await http.get(Uri.parse('https://www.Metaweather.com/API/LOCATIOn/2295420/'));
var r = Json.decode(res.body);
setState(() {
ListModel = ListModel.fromJson({"data": r});
print(ListModel);
circular = false;
});
//
}
@overrIDe
Widget build(BuildContext context) {
// Todo: implement build
return MaterialApp(
home: Scaffold(
appbar: Appbar(
title: Text('WEATHER REPORT'),),body: Center(
child: circular ? CircularProgressInDicator(): ListVIEw.builder(itemCount: ListModel.consolIDateDWeather.length,itemBuilder: (BuildContext context,int indeX)=>
dataShow(ListModel.consolIDateDWeather[index],indeX)),)
),);
}
Widget dataShow(Model obj,indeX) {
return padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0,vertical: 10),child: Container(
height: 100,// wIDth: Mediaquery.of(context).size.wIDth,child: Card(
color: colors.teal,child: column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
Text(
"name ${index + 1} : ${obj.weatherStatename}",style: TextStyle(FontSize: 18,color: colors.whitE),SizedBox(
height: 10,Text(
"Phonenumber : ${obj.theTemp}",],);
}
}
列出 Model.dart
import 'package:weather_app/model.dart';
class ListModel {
ListModel({
required this.consolIDateDWeather,});
List<Model> consolIDateDWeather;
factory ListModel.fromJson(Map<String,dynamic> Json) => ListModel(
consolIDateDWeather: List<Model>.from(Json["consolIDated_weather"].map((X) => Model.fromJson(X))).toList(),);
Map<String,dynamic> toJson() => {
"consolIDated_weather": List<dynamic>.from(consolIDateDWeather.map((X) => x.toJson())),};
}
class Model {
Model({
required this.weatherStatename,required this.weatherStateAbbr,required this.applicableDate,required this.theTemp,});
String weatherStatename;
String weatherStateAbbr;
datetiR_764_11845@e applicableDate;
double theTemp;
factory Model.fromJson(Map<String,dynamic> Json) => Model(
weatherStatename: Json["weather_state_name"],weatherStateAbbr: Json["weather_state_abbr"],applicableDate: datetiR_764_11845@e.parse(Json["applicable_date"]),theTemp: Json["the_temp"].todouble(),dynamic> toJson() => {
"weather_state_name": weatherStatename,"weather_state_abbr": weatherStateAbbr,"applicable_date": "${applicableDate.year.toString().padleft(4,'0')}-${applicableDate.month.toString().padleft(2,'0')}-${applicableDate.day.toString().padleft(2,'0')}","the_temp": theTemp,};
}
pupspec.yml
name: weather_app
description: A new Flutter project.
# The following line prevents the package from being accIDentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots,like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overrIDden in Flutter
# build by specifying --build-name and --build-number,respectively.
# In AndroID,build-name is used as versionname while build-number used as versionCode.
# Read more about AndroID versioning at https://developer.androID.com/studio/publish/versioning
# In iOS,build-name is used as CFBundleShortVersionString while build-number used as CFBundLeversion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPListKeyReference/Articles/CoreFoundationKeys.HTML
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
dependencIEs:
Meta: ^1.0.2
Flutter:
sdk: Flutter
# The following adds the CuperTino Icons Font to your application.
# Use with the CuperTinoIcons class for iOS style icons.
cuperTino_icons: ^1.0.2
http: ^0.13.3
dev_dependencIEs:
Flutter_test:
sdk: Flutter
# For information on the generic Dart part of this file,see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
Flutter:
# The following line ensures that the Material Icons Font is
# included with your application,so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application,add an assets section,like this:
# assets:
# - images/a_doT_Burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants",see
# https://Flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencIEs,see
# https://Flutter.dev/assets-and-images/#from-packages
# To add custom Fonts to your application,add a Fonts section here,# in this "Flutter" section. Each entry in this List should have a
# "family" key with the Font family name,and a "Fonts" key with a
# List giving the asset and other descriptors for the Font. For
# example:
# Fonts:
# - family: Schyler
# Fonts:
# - asset: Fonts/Schyler-Regular.ttf
# - asset: Fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# Fonts:
# - asset: Fonts/TrajanPro.ttf
# - asset: Fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding Fonts from package dependencIEs,# see https://Flutter.dev/custom-Fonts/#from-packages
错误
Performing hot restart...
Syncing files to device Redmi Note 9 Pro...
Restarted application in 1,965ms.
E/Flutter (26913): [ERROR:Flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: NoSuchMethodError: The method 'map' was called on null.
E/Flutter (26913): Receiver: null
E/Flutter (26913): TrIEd calling: map(Closure: (dynamiC) => Model)
E/Flutter (26913): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
E/Flutter (26913): #1 new ListModel.fromJson (package:weather_app/ListModel.dart:11:72)
E/Flutter (26913): #2 _MyAppState.getData.<anonymous closure> (package:weather_app/main.dart:30:29)
E/Flutter (26913): #3 State.setState (package:Flutter/src/Widgets/framework.dart:1088:30)
E/Flutter (26913): #4 _MyAppState.getData (package:weather_app/main.dart:29:5)
E/Flutter (26913): <asynchronous suspension>
E/Flutter (26913):
W/Choreographer(26913): Frame time is 0.009381 ms in the future! check that graphics HAL is generaTing vsync timestamps using the correct timebase.
W/Choreographer(26913): Frame time is 0.021302 ms in the future! check that graphics HAL is generaTing vsync timestamps using the correct timebase.
W/Choreographer(26913): Frame time is 0.006804 ms in the future! check that graphics HAL is generaTing vsync timestamps using the correct timebase.
W/Choreographer(26913): Frame time is 0.064763 ms in the future! check that graphics HAL is generaTing vsync timestamps using the correct timebase.
接下来我可以尝试什么来解决这个问题?
getData() fn 应该像这样..
void getData() async {
var res = await http.get(Uri.parse('https://www.metaweather.com/api/LOCATIOn/2295420/'));
var r = json.decode(res.body);
setState(() {
listModel = ListModel.fromJson(r); // Change this line
print(listModel);
circular = false;
});
}
以上是大佬教程为你收集整理的获取“未处理的异常:NoSuchMethodError:方法'map'被调用为空”全部内容,希望文章能够帮你解决获取“未处理的异常:NoSuchMethodError:方法'map'被调用为空”所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。