0

我正在使用颤振版本 2.2.3 和 graphql_flutter 版本 3.0.0。我的 GraphQL 端点工作正常并以预期的 JSON 响应,但是当我尝试从我的browse_events.dart文件中查询它时,我遇到了这个错误:

The following NoSuchMethodError was thrown building StreamBuilder<QueryResult>-[<'query getAllEvents
{
  allEvents {
    id
    name
    date
    tag
    organizer
    location
  }
}|{}|getAllEvents'>](dirty, state: _StreamBuilderBaseState<QueryResult,
AsyncSnapshot<QueryResult>>#890e2):
The method '[]' was called on null.
Receiver: null
Tried calling: []("allEvents")

The relevant error-causing widget was:
  StreamBuilder<QueryResult>-[<'query getAllEvents {
    allEvents {
      id
      name
      date
      tag
      organizer
      location
    }

任何有关如何解决/修复此问题的建议将不胜感激,如果我应该分享我的代码的任何其他相关部分,请告诉我。

相关代码:

class EventList extends StatefulWidget {
  const EventList({
    Key? key,
  }) : super(key: key);

  @override
  _EventListState createState() => _EventListState();
}

class _EventListState extends State<EventList> {
  final String getAllEvents = """
    query getAllEvents {
      allEvents {
        id
        name
        date  
        tag
        organizer
        location
      }
    }
  """;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        padding: const EdgeInsets.symmetric(horizontal: 8.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            Query(
              options: QueryOptions(
                documentNode: gql(getAllEvents),
                fetchPolicy: FetchPolicy.networkOnly,
              ),
              builder: (QueryResult? result,
                  {VoidCallback? refetch, FetchMore? fetchMore}) {
        
                final events = (result!.data['allEvents']);

                return Expanded(
                  child: ListView.builder(
                    itemCount: events.length,
                    itemBuilder: (BuildContext context, int index) {
                      final event = events[index];
                      return ListTile(
                        title: Text(event['title']),
                        subtitle: Text(event['description']),
                      );
                   ...
4

0 回答 0