0

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): 
1
  • Well, the code part related to this issue would be usefull without having to go through the full repo. About the error, it certainly mean that you try to use a null value somewhere a String is expected. Commented Oct 28, 2022 at 13:53

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 fromJson() 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.