Home > OS >  How to fix Concurrent modification during iteration: Instance(length:41) of '_GrowableList'
How to fix Concurrent modification during iteration: Instance(length:41) of '_GrowableList'

Time:01-02

I can't tell if I'm iterating over each item correctly. nothing gets rendered on the screen and I have the following error that says 'Concurrent modification during iteration: Instance(length:41) of '_GrowableList' I'm not sure how to fix this error and render each item on the screen.. Any ideas?

import 'package:forecast/models/weather_data.dart';

class ForecastData {
  final List list;

  ForecastData({required this.list});

  factory ForecastData.fromJson(Map<String, dynamic> json) {
    var list = json['list']?.map((e) => e)?.toList(growable: true) ?? [];
    list.forEach((e) {
      WeatherData w = WeatherData(
          placeName: json['city']['name'],
          temperature: e['main']['temp'],
          feels_like: e['main']['temp'],
          pressure: e['main']['temp'],
          humidity: e['main']['temp'],
          wind_speed: e['main']['temp']);

      list.add(w);
      print(list);
    });

    return ForecastData(
      list: list,
    );
  }
}

output

Restarted application in 4,038ms.
I/flutter (16409): [{dt: 1641081600, main: {temp: 12.94, feels_like: 12.64, temp_min: 12.8, temp_max: 12.94, pressure: 1014, sea_level: 1014, grnd_level: 1010, humidity: 90, temp_kf: 0.14}, weather: [{id: 803, main: Clouds, description: broken clouds, icon: 04n}], clouds: {all: 75}, wind: {speed: 5.32, deg: 211, gust: 12.51}, visibility: 10000, pop: 0.23, sys: {pod: n}, dt_txt: 2022-01-02 00:00:00}, {dt: 1641092400, main: {temp: 12.86, feels_like: 12.53, temp_min: 12.69, temp_max: 12.86, pressure: 1013, sea_level: 1013, grnd_level: 1008, humidity: 89, temp_kf: 0.17}, weather: [{id: 500, main: Rain, description: light rain, icon: 10n}], clouds: {all: 83}, wind: {speed: 4.85, deg: 224, gust: 12.6}, visibility: 10000, pop: 0.62, rain: {3h: 0.94}, sys: {pod: n}, dt_txt: 2022-01-02 03:00:00}, {dt: 1641103200, main: {temp: 12.71, feels_like: 12.18, temp_min: 12.59, temp_max: 12.71, pressure: 1011, sea_level: 1011, grnd_level: 1007, humidity: 82, temp_kf: 0.12}, weather: [{id: 500, main: Rain, description: light rain, icon: 10n}], cl
E/flutter (16409): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Concurrent modification during iteration: Instance(length:41) of '_GrowableList'.
E/flutter (16409): #0      List.forEach (dart:core-patch/growable_array.dart:411:36)
E/flutter (16409): #1      new ForecastData.fromJson (package:forecast/models/forecast_data.dart:10:10)
E/flutter (16409): #2      _MyAppState.loadForecast.<anonymous closure> (package:forecast/main.dart:121:37)
E/flutter (16409): #3      State.setState (package:flutter/src/widgets/framework.dart:1088:30)
E/flutter (16409): #4      _MyAppState.loadForecast (package:forecast/main.dart:120:14)
E/flutter (16409): <asynchronous suspension>

CodePudding user response:

The error Concurrent modification during iteration means that as you are iterating through the list using the forEach you are also modifying it at the same time by adding new elements and this is not correct.

You can have another local variable of type List<WeatherData> just in the fromJson and in the forEach block just add the new elements to this local variable and use it in the return instead of the list.

CodePudding user response:

in forecast.dart

factory ForecastData.fromJson(Map<String, dynamic> json) {
    var list = json['list']?.map((e) => e)?.toList(growable: true) ?? [];
    List weatherData = [];
    list.forEach((e) {
      WeatherData w = WeatherData(
          placeName: json['city']['name'],
          temperature: e['main']['temp'],
          feels_like: e['main']['temp'],
          pressure: e['main']['temp'],
          humidity: e['main']['temp'],
          wind_speed: e['main']['temp']);

      weatherData.add(w);
      print(weatherData);
    });
    return ForecastData(
      list: list,
    );

After adding weatherData variable, I'm now getting another error that says _InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'WeatherData'..

in weatherData.dart

class WeatherData {
  String? placeName;
  num? temperature;
  num? feels_like;
  num? pressure;
  num? humidity;
  num? wind_speed;

  WeatherData({
    this.placeName,
    this.temperature,
    this.feels_like,
    this.pressure,
    this.humidity,
    this.wind_speed,
  });

  WeatherData.fromJson(Map<String, dynamic> json) {
    placeName = json['city']['name'];
    temperature = json['list'][0]['main']['temp'];
    feels_like = json['list'][0]['main']['temp'];
    pressure = json['list'][0]['main']['temp'];
    humidity = json['list'][0]['main']['temp'];
    wind_speed = json['list'][0]['main']['temp'];
  }
}

output

Another exception was thrown: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'WeatherData'
I/chatty  (21227): uid=10154(com.example.forecast) 1.ui identical 1 line
I/flutter (21227): [Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherData', Instance of 'WeatherDat
  • Related