19

I got this error:

type 'String' is not a subtype of type 'Null' in get method flutter

This is the get API function that I use:

 Future<Stream<ServiceTypes>> getServiceTypesa() async {
 final String url = 'https://test.com';

 final client = new http.Client();
 final streamedRest = await client.send(
   http.Request('get', Uri.parse(url))
 );

 return streamedRest.stream 
     .transform(utf8.decoder)
     .transform(json.decoder)
     .expand((data) => (data as List))
     .map((data) => ServiceTypes.fromJson(data));
}

You can see here the function detect the data:

data from json

I saw also this error:

StateError (Bad state: Stream has already been listened to.)

and this error:

error

This is my listen function:

  @override
  void initState() {
    _serviceTypes = new List<ServiceTypes>();
    listenForServiceTypes();
    super.initState();
  }

  void listenForServiceTypes() async {
    setState(() {
      this._show_serviceTypesProgress = true;
    });
    final Stream<ServiceTypes> stream = await getServiceTypesa();

    stream.listen((ServiceTypes serviceTypes) =>
        setState(() => _serviceTypes.add(serviceTypes)));

    setState(() {
      this._show_serviceTypesProgress = false;
    });

    print(_serviceTypes);
  }

This is the model that T made:

enter image description here

I don't know what is the problem because the print function return : []

4
  • Are you sure the 'deleted_at' is inside the json from your api server? Commented Jan 28, 2020 at 18:52
  • yes i'm sure and i add the model to see it Commented Jan 28, 2020 at 19:21
  • 1
    @Abdullah what theme are you using in the screenshots? Commented Sep 17, 2023 at 21:57
  • @securisec it was long time i forget what was install in my VS code Commented Oct 8, 2023 at 11:06

7 Answers 7

18

When you create model, the image type value is null so, the model is create with Null type of image but when images are not null type from server then it gives you an error

     type 'String' is not a subtype of type 'Null' in get method flutter 

because your image type is String from server and you get value by Null Object, So use String type object for getting string value or images.

Use this

  Class ServiceTypes{
  String image;

instead of

   Class ServiceTypes{
   Null image;
Sign up to request clarification or add additional context in comments.

Comments

10

As extra information, you can check the keys name. I faced the same problem just because I tried to fetch a key not found.

In the first time I wrote content instead of details and in my API I have details not content.

enter image description here

have a good day :)

Comments

5

The issue is clear, you define image type as Null then trying to pass string to it, try to change definition.. like so:

Class ServiceTypes{
  int id;
  String name;
  String image;
...

Comments

2

use ? this for nullable key. For example :

final String? title; final String? category;

here is the complete solution : Is it possible to parse json in Flutter/Dart with missing attributes (keys of json data)?

Comments

1

Step 1: late String? urlToImage; You add '?'

Step 2: urlToImage: json['urlToImage'].toString(),

Comments

0

This kind of error occurs when you create model with pure datatype. In model you give the type to any property such as string,int or any other data-type but assigning it a nullable value. The program through exception that you are assigning a nullable value to non-nullable variable. I have gone through these kind of error in flutter when mapping the firebase data to my model.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

To Convert type toString Eg: Text(jsonList['name].toString());

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.