【问题标题】:type 'int' is not a subtype of type 'double'“int”类型不是“double”类型的子类型
【发布时间】:2020-03-18 01:46:31
【问题描述】:

我的颤振应用程序有这个问题。似乎无法加载 API 请求,但是当我在浏览器上尝试时,它返回正常“http://vplay.id/api/series/popular”。

调试

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

这是我第一次使用颤振,这很有趣,因为只有我在全新安装时遇到这个问题(这是我从 codecanyon 市场获得的应用程序)。

【问题讨论】:

    标签: android flutter dart


    【解决方案1】:

    在这种情况下,一个想法是使用num 而不是intdouble

    【讨论】:

    • 感谢您提供最佳解决方案,您知道 int 和 num 或 double 和 num 之间的区别吗?
    • @Kiax int 和 double 都继承自 num
    • 这是一个很好的解决方案!谢谢
    【解决方案2】:

    您只需将 .toDouble() 函数添加到最后一个返回值。

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

    【讨论】:

      【解决方案3】:
      static double checkDouble(dynamic value) {
          if (value is String) {
            return double.parse(value);
          } else {
            return value;
          }
        }
      }
      

      问题似乎来自最后的return value。你可能需要return value+.0

      【讨论】:

        【解决方案4】:

        正如下面提到的 Gustavo Rodrigues,不要将此作为标准解决方案,这只是一种解决方法。

        我在获取天气数据时遇到了类似的问题。

        我通过将变量声明为 dynamic 而不是 int 类型来解决问题。

        【讨论】:

        • 请不要使用此解决方案。动态将您的类型安全抛到窗外。 paulo conde 的答案 (stackoverflow.com/a/62927320/9695154) 很准确。如果您正在使用需要编组为双倍的 JSON,请在末尾添加 .toDouble。
        【解决方案5】:
        double? checkDouble(dynamic value) {
          if(value is double) return value;
          if(value is int) return value.toDouble();
          if(value is String) return double.tryParse(value);
          return null;
        }
        int? checkInt(dynamic value) {
          if(value is int) return value;
          if(value is double) return value.toInt();
          if(value is String) return int.tryParse(value);
          return null;
        }
        

        【讨论】:

          【解决方案6】:

          在我的例子中,这个错误是因为我必须将我的数字表示为一个双精度值,而不是一个整数值。

          例子:

          将 0 更改为 0.0。

          double rating = rating??0;
          

          到:

          double rating = rating??0.0;
          

          【讨论】:

            【解决方案7】:
             var coordinatesFromJson = json["coordinates"] as List<dynamic>;
            List<double> coordinatesList = [];
            (coordinatesFromJson as List<dynamic>).forEach((element) {
              if (element is int)
                coordinatesList.add((element).toDouble());
              else
                coordinatesList.add(element);
            });
            

            【讨论】:

              【解决方案8】:

              这对你有帮助吗?

              double.parse(rating.toString());
              

              【讨论】:

                【解决方案9】:

                你可以用 double 来初始化你的变量。

                class MyBrick extends StatelessWidget {
                 double x;
                 double y;
                 MyBrick({required this.x, required this.y});
                
                 @override
                 Widget build(BuildContext context) {
                   return Container(
                     alignment: Alignment(x, y),
                     child: ClipRRect(
                       borderRadius: BorderRadius.circular(10),
                       child: Container(
                         color: Colors.white,
                         height: 20,
                         width: MediaQuery.of(context).size.width / 5,
                       ),
                     ),
                   );
                 }
                }
                

                这对我有用...

                【讨论】:

                  猜你喜欢
                  • 2022-10-13
                  • 2021-08-12
                  • 2020-09-29
                  • 2021-05-16
                  • 2020-08-07
                  • 2022-11-24
                  • 2020-10-30
                  • 1970-01-01
                  • 2020-01-04
                  相关资源
                  最近更新 更多