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


Question


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

i saw also this error

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

and this 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

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


Answer 1:


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;



Answer 2:


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;
...


来源:https://stackoverflow.com/questions/59955033/type-string-is-not-a-subtype-of-type-null-in-get-method-flutter


码神部落- 版权声明 1、本主题所有言论和图片纯属会员个人意见,与码神部落立场无关。
2、本站所有主题由该帖子作者发表,该帖子作者一念成空码神部落享有帖子相关版权。
3、码神部落管理员和版主有权不事先通知发贴者而删除本文。
4、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者一念成空码神部落的同意。
5、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任。
6、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责。
7、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意。

最新回复 (0)
    • 码神部落
      2
        立即登录 立即注册 GitHub登录
返回
发新帖
作者最近主题: