【问题标题】:Exception Happened: type 'String' is not a subtype of type 'Mode'发生异常:“字符串”类型不是“模式”类型的子类型
【发布时间】:2022-01-05 16:48:25
【问题描述】:

我最近将一个功能齐全的应用程序转移到 dart null safety 并引发了一些错误

此 API 调用已调整,但现在我收到错误 flutter: Exception Happened: type 'String' is not a subtype of type 'Mode'

我通过获取用户位置的函数调用 API。

重申一下,这在升级 dart 之前运行良好,所以我相信这是适应 null 安全性的问题。

API 调用(抛出异常的地方)

Future<Stations?> fetchStations() async {
    var client = http.Client();
    Stations? stations; 
     
   Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);


    var lat = position.latitude;
    var long = position.longitude;
    
try{
    var response = await client.get(Uri.parse('https_call'));
    if (response.statusCode == 200) {
   var jsonString = response.body;
   var jsonMap = json.decode(jsonString);
   
   stations = Stations.fromJson(jsonMap);
   //print(stations);
  }
} catch(e) {

  print("Exception Happened: ${e.toString()}");
}
 
  return stations; 
}

调用API的函数

Future<void> showAnchoredMapMarkers() async {
  var stations = await fetchStations();
  for (Station stations in stations!.stations) {
    GeoCoordinates geoCoordinates = GeoCoordinates (stations.place.location.lat, stations.place.location.lng);
    var id = stations.place.id;
    
    _addCircleMapMarker(geoCoordinates, 0);
    _addPOIMapMarker(geoCoordinates, 1);
  
  }
}

更新

class Station {
    Station({
        this.place,
        required this.transports,
    });

    Place place;
    List<Transport> transports;

    factory Station.fromJson(Map<String, dynamic> json) => Station(
        place: json["place"] == null ? null : Place.fromJson(json["place"]),
        transports: json["transports"] == null ? null : List<Transport>.from(json["transports"].map((x) => Transport.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "place": place == null ? null : place.toJson(),
        "transports": transports == null ? null : List<dynamic>.from(transports.map((x) => x.toJson())),
    };
}

这样的行

place: json["place"] == null ? null : Place.fromJson(json["place"]),

全部用红色下划线标有错误,如

The argument type 'Place?' can't be assigned to the parameter type 'Place'.

【问题讨论】:

    标签: flutter dart dart-null-safety


    【解决方案1】:

    发生这种情况是因为在您的 Model 文件中,您定义了 mode 的值应该是 Mode,但您将 String 传递给它。简单的解决方案是将模式的datatype 更改为String,如下所示:

    class Transport {
        Transport({
            required this.mode,
            required this.name,
            required this.color,
            required this.textColor,
            required this.headsign,
        });
    
        String mode;
        String name;
        String color;
        TextColor textColor;
        String headsign;
    
       more code ...
    }
    

    【讨论】:

    • 谢谢,这似乎已经修复了那个特定的实例,我现在收到这个异常而不是flutter: Exception Happened: type 'Null' is not a subtype of type 'String'你知道这是为什么吗?谢谢
    • 这是因为您可能从 API 获得空值,而您的模型还没有准备好。您可以通过使该变量像 String 一样可以为空来解决这个问题?刚放?标记,您的错误应该得到解决。
    • 它似乎来自调用 API 的函数中的 for (Station stations in stations!.stations) { 行。错误是_CastError (Null check operator used on a null value)。我必须添加一个! 来修复stations 上的红色下划线,这一定是原因吗?谢谢
    • 表示你没有得到站内的值,你需要检查一下。
    • 我在互联网浏览器中调用 api,它返回的数据很好。我的困惑是因为它在null safety 之前完美运行。我现在刚刚调用了fetch stations 方法来查看返回的结果,什么都没有,是不是因为我做了 json 改编(需要等)
    猜你喜欢
    • 1970-01-01
    • 2020-08-04
    • 2021-04-28
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    相关资源
    最近更新 更多