3

大家好,当用户没有互联网连接但似乎无法正常工作时,我正在尝试使用来自 graphql_flutter 包的缓存提供程序在我的颤振项目中显示我的数据,所以我想知道是否有我遗漏的东西,我需要大家的帮助,感谢您的考虑。

这是我在“main.dart”中的代码

 HttpLink httpLink = HttpLink(
      'http://104.131.161.92/graphql/',
      defaultHeaders: <String, String>{
        'accept-language': MyApp.language == 'es' ? 'rw' : 'en'
      });

  //webSocket link for chat
  final WebSocketLink webSocketLink = WebSocketLink(
    'ws://104.131.161.92:8002/graphql/',
    config: SocketClientConfig(
      initialPayload: () async => 'JWT ${MyApp.token}',
    
    // config: SocketClientConfig(
      autoReconnect: true,
      inactivityTimeout: Duration(
        minutes: 2 
      )
    )
    );

  AuthLink authLink = AuthLink(
    getToken: () async => 'JWT ${MyApp.token}',
  );

  Link link = authLink.concat(httpLink);

  link = Link.split((request) => request.isSubscription, webSocketLink, link);

  ValueNotifier<GraphQLClient> client = ValueNotifier(
    GraphQLClient(
      link: link,
      cache: GraphQLCache(store: HiveStore()),
    ),
  );

  return GraphQLProvider(
    client: client,
    child: CacheProvider(
      child: MultiProvider(
        providers: [
          ChangeNotifierProvider(
            create: (context) => AppState(),
          ),
          FutureProvider<EarnedMarks>(
            initialData: EarnedMarks(0),
            create: (context) async {
              final prefs = await SharedPreferences.getInstance();
              return EarnedMarks(prefs.getDouble('results'));
            },
          ),

        ],
        child: MaterialApp(...

下面是我从后端查询数据的示例文件(all_posts.dart)

Query(
        options: QueryOptions(
          document: gql(allPostsList),
          variables: {'age': MyApp.age, 'gender': MyApp.gender, 'page': page},
          // pollInterval: 10,
        ),
        builder: (QueryResult result,
            {VoidCallback refetch, FetchMore fetchMore}) {
          _refetch = refetch;
          if (result.hasException) {
            print(result.exception.toString());
            return 
                Text(result.exception.toString());
          }
          if (result.isLoading) {
            return Center(child: CircularProgressIndicator());
          }
          
          List posts = result.data['publishedPosts']['objects'];

          int pages = result.data['publishedPosts']['pages'];

          return Container(
4

0 回答 0