Flutter错误“不是'String'类型的子类型

时间:2021-06-28 13:00:54

标签: flutter dart

我正在调用插件中的一个类来获取地址列表,当我点击一个时,我想在我的 import line_profiler profile = line_profiler.LineProfiler() @profile def my_function_in_django_code(): .... with open('output.txt', 'w') as stream: profile.print_stats(stream=stream) return something 插件上显示它。我可以做所有但实际上在 TypeAheadField 的 texfield 中显示所选文本。这是我的代码

TypeAheadField

TypeAheadWidget 用法

class SearchScreen extends StatefulWidget {
  @override
  _SearchScreenState createState() => _SearchScreenState();
}

class _SearchScreenState extends State<SearchScreen> {
 TextEditingController dropOffTextEditingController = TextEditingController();

现在是 TypeAhead 列表来自插件中的类

TypeAheadField(
    textFieldConfiguration: TextFieldConfiguration(
       ..............
    onSuggestionSelected: (suggestion) {
        print(suggestion);
        dropOffTextEditingController.text =
            suggestion;
    }),

控制台上的错误

import 'geo_point.dart';

class Address {
  final String? postcode;
  final String? name;
  final String? street;
  final String? city;
  final String? state;
  final String? country;

  Address({
    this.postcode,
    this.street,
    this.city,
    this.name,
    this.state,
    this.country,
  });

  Address.fromPhotonAPI(Map data)
      : this.postcode = data["postcode"],
        this.name = data["name"],
        this.street = data["street"],
        this.city = data["city"],
        this.state = data["state"],
        this.country = data["country"];

  @override
  String toString() {
    String addr = "";
    if (name != null && name!.isNotEmpty) {
      addr = addr + "$name,";
    }
    if (street != null && street!.isNotEmpty) {
      addr = addr + "$street,";
    }
    if (postcode != null && postcode!.isNotEmpty) {
      addr = addr + "$postcode,";
    }
    if (city != null && city!.isNotEmpty) {
      addr = addr + "$city,";
    }
    if (state != null && state!.isNotEmpty) {
      addr = addr + "$state,";
    }
    if (country != null && country!.isNotEmpty) {
      addr = addr + "$country";
    }

    return addr;
  }
}

class SearchInfo {
  final GeoPoint? point;
  final Address? address;

  SearchInfo({
    this.point,
    this.address,
  });

  SearchInfo.fromPhotonAPI(Map data)
      : this.point = GeoPoint(
            latitude: data["geometry"]["coordinates"][1],
            longitude: data["geometry"]["coordinates"][0]),
        this.address = Address.fromPhotonAPI(data["properties"]);
}

如何在不影响插件的情况下编辑我的代码?

1 个答案:

答案 0 :(得分:0)

错误来自这一行

 dropOffTextEditingController.text =
            suggestion;

这就是我们需要做的:

 dropOffTextEditingController.text =
            (suggestion as SearchInfo)
                .address
                .name;