NoSuchMethodError:方法“ forEach”在null上调用

时间:2020-09-22 19:00:35

标签: json flutter dio

用Dio提取数据后出现错误。

import numpy as np

在此行Exception has occurred. NoSuchMethodError (NoSuchMethodError: The method 'forEach' was called on null. Receiver: null Tried calling: forEach(Closure: (String, dynamic) => Null)) 上出现上述错误

这就是我从json获取数据的方式:

jsonResponse.forEach((key, value) {

}

这是我的airquality_model.dart

    Future<List<AirQualityModel>> _getAirQuality() async {
var dio = Dio();
var cookieJar;
Response dioResponse;
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
try {
  cookieJar = PersistCookieJar(dir: appDocPath + "/.cookies/");
  dio.interceptors.add(CookieManager(cookieJar));
  dioResponse = await dio.get(
      "https://www.airvisual.com/api/v2/node/5ded3e13994dfe107f7013a0");
  debugPrint('dioResponse: ' + dioResponse.data.toString());
  debugPrint("response: " + dioResponse.statusMessage.toString());
  jsonResponse = json.decode(dioResponse.data);
  airqualityList = List<AirQualityModel>();
  jsonResponse?.forEach((key, value) {
    airqualityList = (jsonResponse['current'] as List)
        .map<AirQualityModel>((j) => AirQualityModel.fromJson(j))
        .toList();
  });
  debugPrint('List: $airqualityList');
  return airqualityList;
} catch (e) {
  print('catch error: $e');
}

这是我的调试控制台:

enter image description here

我已经更新了代码,并设置了解码json的状态,现在调试控制台看起来像这样:

class AirQualityModel {
final int pm25;
final int co2;
final int humidity;
final int temperature;

AirQualityModel(this.pm25, this.co2, this.humidity, this.temperature);

AirQualityModel.fromJson(Map<String, dynamic> json)
  : pm25 = json['p2'],
    co2 = json['co'],
    humidity = json['hm'],
    temperature=json['tp'];
}

如何解决此错误?

2 个答案:

答案 0 :(得分:1)

您可以在下面复制粘贴运行完整代码
步骤1:dioResponse.dataMap,则无需再次执行json.decode
步骤2:dioResponse.data["current"]不是List,您可以直接使用Future<AirQualityModel>

代码段

Future<AirQualityModel> _getAirQuality() async {
    var dio = Dio();
    var cookieJar;
    Response dioResponse;
    Directory appDocDir = await getApplicationDocumentsDirectory();
    String appDocPath = appDocDir.path;
    try {
      cookieJar = PersistCookieJar(dir: appDocPath + "/.cookies/");
      dio.interceptors.add(CookieManager(cookieJar));
      dioResponse = await dio.get(
          "https://www.airvisual.com/api/v2/node/5ded3e13994dfe107f7013a0");

      return AirQualityModel.fromJson(dioResponse.data["current"]);
    } catch (e) {
      print('catch error: $e');
    }
  }

工作演示

enter image description here

完整代码

import 'dart:convert';
import 'dart:io';

import 'package:cookie_jar/cookie_jar.dart';
import 'package:dio/dio.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

class AirQualityModel {
  final double pm25;
  final double co2;
  final double humidity;
  final double temperature;

  AirQualityModel(this.pm25, this.co2, this.humidity, this.temperature);

  AirQualityModel.fromJson(Map<String, dynamic> json)
      : pm25 = json['p2'].toDouble(),
        co2 = json['co'].toDouble(),
        humidity = json['hm'].toDouble(),
        temperature = json['tp'].toDouble();
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Future<AirQualityModel> _future;

  Future<AirQualityModel> _getAirQuality() async {
    var dio = Dio();
    var cookieJar;
    Response dioResponse;
    Directory appDocDir = await getApplicationDocumentsDirectory();
    String appDocPath = appDocDir.path;
    try {
      cookieJar = PersistCookieJar(dir: appDocPath + "/.cookies/");
      dio.interceptors.add(CookieManager(cookieJar));
      dioResponse = await dio.get(
          "https://www.airvisual.com/api/v2/node/5ded3e13994dfe107f7013a0");

      return AirQualityModel.fromJson(dioResponse.data["current"]);
    } catch (e) {
      print('catch error: $e');
    }
  }

  @override
  void initState() {
    _future = _getAirQuality();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: FutureBuilder(
          future: _future,
          builder: (context, AsyncSnapshot<AirQualityModel> snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.none:
                return Text('none');
              case ConnectionState.waiting:
                return Center(child: CircularProgressIndicator());
              case ConnectionState.active:
                return Text('');
              case ConnectionState.done:
                if (snapshot.hasError) {
                  return Text(
                    '${snapshot.error}',
                    style: TextStyle(color: Colors.red),
                  );
                } else {
                  return Card(
                      elevation: 6.0,
                      child: Padding(
                        padding: const EdgeInsets.only(
                            top: 6.0, bottom: 6.0, left: 8.0, right: 8.0),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Text("co2  ${snapshot.data.co2.toString()}"),
                            Spacer(),
                            Text(
                              "pm25 ${snapshot.data.pm25.toString()}",
                            ),
                          ],
                        ),
                      ));
                  ;
                }
            }
          }),
    );
  }
}

答案 1 :(得分:0)

jsonResponse可能为null,因此请在使用它之前进行检查,例如jsonResponse?.forEach((key, value)