You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用graphql_flutter遇GraphQLWebSocketChannel未实现ready成员错误求助

问题

在基于Stacked框架的MVVM架构中使用graphql_flutter包时,运行代码触发如下错误:

../../Developer/flutter/.pub-cache/hosted/pub.dartlang.org/graphql-5.1.2/lib/src/links/websocket_link/websocket_client.dart:577:7: Error: The non-abstract class 'GraphQLWebSocketChannel' is missing implementations for these members:
- WebSocketChannel.ready
Try to either
- provide an implementation,
- inherit an implementation from a superclass or mixin,
- mark the class as abstract, or
- provide a 'noSuchMethod' implementation.

class GraphQLWebSocketChannel extends StreamChannelMixin<dynamic>
^^^^^^^^^^^^^^^^^^^^^^^
../../Developer/flutter/.pub-cache/hosted/pub.dartlang.org/web_socket_channel-2.3.0/lib/src/channel.dart:56:22: Context: 'WebSocketChannel.ready' is defined here.
final Future<void> ready = Future.value();

即使仅导入该包也会出现此错误,以下是main.dart代码:

import 'package:flutter/material.dart';
import 'package:testing/app/app.locator.dart';
import 'package:testing/ui/common/app_colors.dart';
import 'package:testing/ui/setup/setup_bottom_sheet_ui.dart';
import 'package:testing/ui/setup/setup_dialog_ui.dart';
import 'package:stacked_services/stacked_services.dart';

import 'package:graphql_flutter/graphql_flutter.dart';

import 'app/app.router.dart';

void main() {
  setupLocator();
  setupDialogUi();
  setupBottomSheetUi();

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    HttpLink httpLink = HttpLink("https://api.github.com/graphql");

    AuthLink authLink = AuthLink(
      getToken: () async => 'Bearer PERSONAL_ACCESS_TOKEN',
    );

    Link link = authLink.concat(httpLink);

    ValueNotifier<GraphQLClient> qlClient = ValueNotifier(
      GraphQLClient(
        link: link,
        // The default store is the InMemoryStore, which does NOT persist to disk
        cache: GraphQLCache(store: HiveStore()),
      ),
    );

    return GraphQLProvider(
        client: qlClient,
        child: MaterialApp(
          title: 'Flutter Demo',
          theme: Theme.of(context).copyWith(
            primaryColor: kcBackgroundColor,
            focusColor: kcPrimaryColor,
            textTheme: Theme.of(context).textTheme.apply(
                  bodyColor: Colors.black,
                ),
          ),
          initialRoute: Routes.startupView,
          onGenerateRoute: StackedRouter().onGenerateRoute,
          navigatorKey: StackedService.navigatorKey,
          navigatorObservers: [
            StackedService.routeObserver,
          ],
        ));
  }
}
解决方案

这个错误是由于graphql包(graphql_flutter的依赖)与web_socket_channel包版本不兼容导致的:web_socket_channel2.3.0新增了ready成员,但旧版本的graphql包未实现该接口。可通过以下方式解决:

  • 升级graphql_flutter到兼容版本
    打开pubspec.yaml,将graphql_flutter的版本更新至5.2.0及以上(该版本已适配web_socket_channel2.3.0的新API),执行flutter pub get更新依赖。

  • 锁定web_socket_channel的版本
    若暂时不想升级graphql_flutter,可在pubspec.yaml中添加约束,将web_socket_channel锁定到2.2.0版本:

    dependencies:
      # 其他依赖项保持不变
      web_socket_channel: 2.2.0
    

    执行flutter pub get完成版本降级。

  • 清理缓存重新安装依赖
    执行以下命令清理Flutter缓存并重新获取依赖:

    flutter clean
    flutter pub get
    

内容的提问来源于stack exchange,提问作者alexa tang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 07:45:29