小编Hen*_*elo的帖子

使用 Ferry 和 Flutter 添加标头以进行请求

这是我第一次使用 Ferry 发出 GraphQL 请求。我的 GraphQL Server 有一些查询需要 HTTP 标头进行授权。

我需要能够在初始化客户端后添加标头。

客户端.dart

Future<Client> initClient() async {
  await Hive.initFlutter();

  final box = await Hive.openBox<Map<String, dynamic>>("graphql");

  await box.clear();

  final store = HiveStore(box);

  final cache = Cache(store: store);

  final link = HttpLink("example.com/");

  final client = Client(
    link: link,
    cache: cache,
  );

  return client;
}
Run Code Online (Sandbox Code Playgroud)

主.dart

void main() async{
  final client = await initClient();
  GetIt.I.registerLazySingleton<Client>(() => client);
  runApp(MyApp());
}
Run Code Online (Sandbox Code Playgroud)

请求文件

    client.request(Req).listen((response) {
      print(response.graphqlErrors); // It will return an error because theres …
Run Code Online (Sandbox Code Playgroud)

dart graphql flutter

4
推荐指数
1
解决办法
2426
查看次数

标签 统计

dart ×1

flutter ×1

graphql ×1