Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 i made

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

like image 543
Abdullah Avatar asked Dec 17 '22 14:12

Abdullah


1 Answers

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;
like image 118
Avinash Avatar answered Dec 29 '22 03:12

Avinash