I would be highly grateful if the answer is in simple terms. I am a beginner.
here is my full project code: https://drive.google.com/file/d/1nj7Pj7Gzl-xVzzCm-aPRu2H1QUvdjoZB/view?usp=sharing
My error is:
E/flutter (17497): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'String'
E/flutter (17497): #0 new Welcome.fromJson (package:getx_5_rest_api/models/product.dart:33:22)
E/flutter (17497): #1 welcomeFromJson.<anonymous closure> (package:getx_5_rest_api/models/product.dart:10:60)
E/flutter (17497): #2 MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31)
E/flutter (17497): #3 ListIterator.moveNext (dart:_internal/iterable.dart:342:26)
E/flutter (17497): #4 new List.from (dart:core-patch/array_patch.dart:41:17)
E/flutter (17497): #5 welcomeFromJson (package:getx_5_rest_api/models/product.dart:10:5)
E/flutter (17497): #6 RemoteServices.fetchProducts (package:getx_5_rest_api/services/remote_service.dart:31:14)
E/flutter (17497): <asynchronous suspension>
E/flutter (17497): #7 ProductController.fetchProducts (package:getx_5_rest_api/controllers/product_controller.dart:21:20)
E/flutter (17497): <asynchronous suspension>
E/flutter (17497):
CodePudding user response:
I found your problem
in the JSON response there is an image_link
field
but in your code inside the :
factory Welcome.fromJson(Map<String, dynamic> json) => Welcome(
name: json["name"],
price: json['price'],
imageLink: json['imageLink'].toString(),
rating: json['rating']);
}
you are trying to get imageLink
which doesn't exist in the JSON maps List, so it throws null, what exists is image_link
and they are not the same so the solution is this :
replace the fromJson
method with this
factory Welcome.fromJson(Map<String, dynamic> json) => Welcome(
name: json["name"],
price: json['price'],
imageLink: json['image_link'].toString(),
rating: json['rating']);
}
it should now find it.
Hope this helps you.
CodePudding user response:
in line 22 in product model, you probably entered wrong property in the map,
check the response from the API and make sure all the properties match in your
fromMap()
function
you should use GitHub to share your code, it'll be easier for others to help you when they can just browse the code as easy.