Search code examples
flutterdartdart-null-safety

Exception has occurred. _TypeError (type 'Null' is not a subtype of type 'Iterable<dynamic>')


I am building an ecommerce app, I created the login screen in such a way that when a user enters his credentials and clicks on the login button all this information below will show(id, name, email, password, adress, type, token, cart), the application runs alright, but when i try to login with my credential this error'('Exception has occurred. _TypeError (type 'Null' is not a subtype of type 'Iterable'))' pops up on my code particularly in the area where 'cart' is found. when i comment out the 'cart' the error doesnot popup again, i am able to read the credential normally when i login

import 'dart:convert';
    
    class User {
      final String id;
      final String name;
      final String email;
      final String password;
      final String address;
      final String type;
      final String token;
      final List<dynamic> cart;
    
      User({
        required this.id,
        required this.name,
        required this.email,
        required this.password,
        required this.address,
        required this.type,
        required this.token,
        required this.cart,
      });
    
      Map<String, dynamic> toMap() {
        return {
          'id': id,
          'name': name,
          'email': email,
          'password': password,
          'address': address,
          'type': type,
          'token': token,
          'cart': cart,
        };
      }
    
      factory User.fromMap(Map<String, dynamic> map) {
        return User(
          id: map['_id'] ?? '',
          name: map['name'] ?? '',
          email: map['email'] ?? '',
          password: map['password'] ?? '',
          address: map['address'] ?? '',
          type: map['type'] ?? '',
          token: map['token'] ?? '',
          cart: List<Map<String, dynamic>>.from(
            map['cart']?.map(
              (x) => Map<String, dynamic>.from(x), **...where my error appears...**
            ),
          ),
        );
      }
    
      String toJson() => json.encode(toMap());
    
      factory User.fromJson(String source) => User.fromMap(json.decode(source));
    
      User copyWith({
        String? id,
        String? name,
        String? email,
        String? password,
        String? address,
        String? type,
        String? token,
        List<dynamic>? cart,
      }) {
        return User(
          id: id ?? this.id,
          name: name ?? this.name,
          email: email ?? this.email,
          password: password ?? this.password,
          address: address ?? this.address,
          type: type ?? this.type,
          token: token ?? this.token,
          cart: cart ?? this.cart,
        );
      }
    }

Solution

  • Change this

     cart: List<Map<String, dynamic>>.from(
        map['cart']?.map(
          (x) => Map<String, dynamic>.from(x),
        ),
      ),
    

    to this

     cart: List<Map<String, dynamic>>.from(
        map['cart']?.map(
          (x) => Map<String, dynamic>.from(x), 
        ) ?? [],
      ),
    

    If map['cart'] is null then you are creating List from null that's why you are getting error. You have to pass empty list in case of null.