Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type 'int' is not a subtype of type 'double'

I have this issue on my flutter application. It seems failed to load an API request, but when i try on browser it return okay "http://vplay.id/api/series/popular".

Debug

exception = {_Exception} Exception: Failed to load
message = "Failed to load"
this = {SeriesProvider} 
 _url = "http://vplay.id/api/"
 _loading = false
 _currentPage = 1
 _pages = 1
 _series = {_GrowableList} size = 0
 _seriesStream = {_AsyncStreamController} 
endpoint = "series/popular"
e = {_TypeError} type 'int' is not a subtype of type 'double'
 _stackTrace = {_StackTrace} 
 _failedAssertion = "is assignable"
 _url = "package:streamapp/src//helpers/parse.dart"
 _line = 31
 _column = 7
 message = "type 'int' is not a subtype of type 'double'"

Series_provider.dart

import 'dart:async';
import 'dart:convert';

import 'package:http/http.dart' as http;
import '../helpers/api.dart';
import '../models/serie_model.dart';
export '../models/serie_model.dart';

class SeriesProvider {
  String _url = Api.url;
  bool _loading = false;
  int _currentPage = 1;
  int _pages = 1;
  List<Serie> _series = List();
  final _seriesStream = StreamController<List<Serie>>();

  Function(List<Serie>) get seriesSink => _seriesStream.sink.add;
  Stream<List<Serie>> get seriesStream => _seriesStream.stream;

  void dispose() {
    _seriesStream?.close();
  }

  Future<List<Serie>> _process(String endpoint) async {
    try {
      final resp = await http.get(_url + endpoint);

      if (resp.statusCode != 200) {
        throw Exception('Failed to load');
      }

      final data = json.decode(resp.body);

      final series = Series.fromJsonList(data);

      return series.items;
    } catch (e) {
      throw Exception('Failed to load');
    }
  }

Helpers/parse.dart

static double checkDouble(dynamic value) {
    if (value is String) {
      return double.parse(value);
    } else {
      return value;
    }
  }
}

This is my first time with flutter, and this is interesting since it only me face this issue (this is an app i get from codecanyon marketplace) with fresh installation.

like image 992
Mahar Dika Avatar asked Nov 22 '19 02:11

Mahar Dika


People also ask

Is int a subtype of Double?

android - type 'int' is not a subtype of type 'double' - Stack Overflow.

What is double in flutter?

A double is a floating-point number, or a number with a decimal point.

Is not a subtype of type String flutter?

To Solve type 'int' is not a subtype of type 'String' error in Dart Error is saying you are passing String where flutter required for the Integer value. So that you have to pass Integer there Or you can just use our solutions to solve this error.


3 Answers

An idea would be to use num instead of int or double in this case.

like image 65
larsaars Avatar answered Oct 20 '22 17:10

larsaars


You only need to add .toDouble() function to last returned value.

static double checkDouble(dynamic value) {
    if (value is String) {
      return double.parse(value);
    } else {
      return value.toDouble();
    }
  }
}
like image 15
Paulo Conde Avatar answered Oct 20 '22 16:10

Paulo Conde


static double checkDouble(dynamic value) {
    if (value is String) {
      return double.parse(value);
    } else {
      return value;
    }
  }
}

The problem seems from the last return value. You may need return value+.0.

like image 13
Wenbo Avatar answered Oct 20 '22 18:10

Wenbo