NoSuchMethodError: Class ‘List’ has no instance method ‘Map’.
Receiver: Instance(length:0) of ‘_GrowableList’
Tried calling: Map(Closure: (dynamic) => Map<String, dynamic>)
you may look here
i am getting this when i enter my correct information even i use print state ment i am getting the correct info from database but getting this problem and if i enter wrong if in the console i am getting wrong password or user not fount but the is on other eror this is the error that iam getting when i enter wrong info click here to see type ‘Null’ is not a subtype of type ‘String
i added context .mounted in my flutter codes
My user Model
// ignore_for_file: public_member_api_docs, sort_constructors_first
class User {
final String id;
final String name;
final String email;
final String password;
final bool isAdmin;
final int phone;
final String address;
final List<dynamic>? cart;
final List<dynamic>? wishlist;
User({
required this.id,
required this.name,
required this.email,
required this.password,
required this.isAdmin,
required this.phone,
required this.address,
this.cart,
this.wishlist,
});
Map<String, dynamic> toMap() {
return <String, dynamic>{
'id': id,
'name': name,
'email': email,
'password': password,
'isAdmin': isAdmin,
'phone': phone,
'address': address,
'cart': cart,
'wishlist': wishlist,
};
}
factory User.fromMap(Map<String, dynamic> map) {
return User(
id: map['_id'] ?? '',
name: map['name'] ?? '',
email: map['email'] ?? '',
password: map['password'] ?? '',
isAdmin: map['isAdmin'] ?? false,
phone: map['phone'] ?? 0,
address: map['address'] ?? '',
cart: List<Map<String, dynamic>>.from((map['cart']?.Map((x)=> Map<String,dynamic>.from(x)) )) ,
wishlist: List<Map<String,dynamic>>.from((map['wishlist']?.Map((x)=> Map<String, dynamic>.from(x))),
));
}
String toJson() => json.encode(toMap());
factory User.fromJson(String source) => User.fromMap(json.decode(source) as Map<String, dynamic>);
User copyWith({
String? id,
String? name,
String? email,
String? password,
bool? isAdmin,
int? phone,
String? address,
List<dynamic>? cart,
List<dynamic>? wishlist,
}) {
return User(
id: id ?? this.id,
name: name ?? this.name,
email: email ?? this.email,
password: password ?? this.password,
isAdmin: isAdmin ?? this.isAdmin,
phone: phone ?? this.phone,
address: address ?? this.address,
cart: cart ?? this.cart,
wishlist: wishlist ?? this.wishlist,
);
}
}
my login function
required BuildContext context,
required String email,
required String password,
}) async {
try {
http.Response res = await http.post(Uri.parse('$url/api/users/login'),
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"email": email,
"password": password,
}));
print(res.statusCode);
print(res.body);
SharedPreferences preferences = await SharedPreferences.getInstance();
preferences.setString('userId', jsonDecode(res.body)['_id']);
if (context.mounted) {
Provider.of<Userprovider>(context, listen: false).setUser(res.body);
}
if (res.statusCode == 200) {
if (context.mounted) {
showSnack(context, "Welcome");
bool isAdmin = jsonDecode(res.body)['isAdmin'];
isAdmin
? Navigator.pushNamed(context, AdminScreen.routeName)
: Navigator.pushNamed(context, HomeScreen.routeName);
}
} else {
if (context.mounted) {
showSnack(context, res.body);
}
}
} catch (e) {
if (context.mounted) {
showSnack(context, e.toString());
}
}
}
my user provider
import 'package:softiweer_shop/models/user_model.dart';
class Userprovider extends ChangeNotifier {
User users = User(
id: '',
name: '',
email: '',
password: '',
isAdmin: false,
phone: 0,
address: '',
cart: [],
wishlist: []);
User get _user => users;
void setUser(String user) {
users = User.fromJson(user);
notifyListeners();
}
void setFromModel(User user) {
users = user;
notifyListeners();
}
}
MOHAMED AMIIN ABDI AADAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2