如何在Flutter中使用graphql从服务器获取数据?

时间:2019-06-10 09:48:38

标签: flutter dart graphql

  

我正在尝试从server的数据中获取数据。我试图使   列表并显示在屏幕上。但是我没有前进的念头。在   body,我还添加了对graphql的查询。

     

类优惠扩展了StatelessWidget {
               @override Widget build(BuildContext context){                   **最终HttpLink httpLink =                       HttpLink(uri:'link');

            final AuthLink authLink = AuthLink(
            getToken: () async => 'Bearer <token>',**
            // OR
            // getToken: () => 'Bearer 
       <YOUR_PERSONAL_ACCESS_TOKEN>',   
           );   final Link link = authLink.concat(httpLink as 
           Link);
            final ValueNotifier<GraphQLClient> client =
                ValueNotifier<GraphQLClient>(GraphQLClient(
                    link: link,
                    cache: InMemoryCache()));
            return GraphQLProvider(
              child: OfferList(),
              client: client,
            );   } }

        class OfferList extends StatefulWidget {   @override   
     _OfferListState createState() => _OfferListState(); }

        class _OfferListState extends State<OfferList> {   
       @override   
       Widget build(BuildContext context) {
            // TODO: implement build
            return Scaffold(
              appBar: AppBar(
                title: Text("Offers"),
                backgroundColor: Colors.indigoAccent,
              ),
                 body: Query(
                  options: QueryOptions(document: """
                query(
                  affOffers{
                    edges {
                      node {
                        id  
                        name  
                        status  
                        description  
                        end_time  
                        currency  
                      }
                    }
                  }
                  }
                   """),
                    builder: (
                     QueryResult result, {
                      VoidCallback refetch,
                  }) {
                    if (result.data == null) {
                      return Text("No data found");
                    }
                    //return Text("Success");

                    List offerLists = result.data['affOffers'] 
  ['edges'] 
                ['node'];
                    print(offerLists.length);
                    return ListView.builder(

                      itemBuilder: (BuildContext context, int 
             index) {
                        final offer = offerLists[index];
                        return Text(offer['name']);
                      },
                      itemCount: offerLists.length,
                    );
                  }),
            );   
}     }

0 个答案:

没有答案