错误状态:BackgroundIsolateBinaryMessenger.instance 值在执行BackgroundIsolateBinaryMessenger.ensureInitialized 之前无效

2024-03-13

  static getMaxId(SendPort sendPort) async {
    ApiService apiService = ApiService();
    var maxId = await apiService.getMId("1");
    sendPort.send(getFeedData(maxId));
  }

  static getFeedData(dynamic maxId) async {
    ApiService apiService = ApiService();
    List<MarketfeedsModel>? marketFeedsNew =
        await apiService.getfH("1", maxId.toString());
    return marketFeedsNew;
  }

  var receivePort = ReceivePort();
  await Isolate.spawn(getMaxId, receivePort.sendPort);

  receivePort.listen((message) {
   print("data " + message.toString());
  });

Future<dynamic> getMId(String tabletype) async {
    final api = '$baseUrl/api/Feeds/getMaxId';
    Map<String, dynamic> queryParameters = {'tableid': tabletype};
    try {
      Response response;
      Dio dio = Dio();
      await apiClientToken().then((value) => {dio = value});
      response = await dio.get(api, queryParameters: queryParameters);
      final body = response.data;
      if (response.statusCode == 200) {
        if (body != null) {
          return body['message'];
        } else {
          return null;
        }
      } else {
        print(response.data);
        return null;
      }
    } on DioError catch (e) {
      // setError(e, context);
    } catch (e) {
      print(e);
    }
    return null;
  }

我正在尝试在我的代码中实现隔离。一切正常,直到使用 Isolate。使用 Isolate 后,无法从 api 获取数据,并且显示错误并从 api 获取空响应。 我不知道为什么我总是收到 bad state BackgroundIsolateBinaryMessenger 错误。

问题就在这里

static getMaxId(SendPort sendPort) async {
    ApiService apiService = ApiService();
    var maxId = await apiService.getMId("1");
    sendPort.send(getFeedData(maxId));
  }

使用 sendport 时总是获取 maxId null 如果 sendport 没有使用执行普通代码然后获取 maxId


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误状态:BackgroundIsolateBinaryMessenger.instance 值在执行BackgroundIsolateBinaryMessenger.ensureInitialized 之前无效 的相关文章

随机推荐