Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoSuchMethodError (NoSuchMethodError: The getter 'host' was called on null #586

Closed
diegothucao opened this issue Mar 20, 2020 · 21 comments
Closed

Comments

@diegothucao
Copy link

when we call api in case internet is off, it crash because of issue

NoSuchMethodError (NoSuchMethodError: The getter 'host' was called on null

And seem we cannot catch exception.
Could you check on it?

@staaouat
Copy link

staaouat commented Mar 20, 2020

I get the same problem by running the following demo:

====flutter run --verbose
[+3951 ms] E/flutter (23811): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'host' was called on null.

my code
import 'package:flutter/material.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import './mutations.dart' as mutations;
import './queries.dart' as queries;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final HttpLink httpLink = HttpLink(
     uri:'https://countries.trevorblades.com/', 
    );

    final ValueNotifier<GraphQLClient> client = ValueNotifier<GraphQLClient>(
      GraphQLClient(
        cache: InMemoryCache(),
        link: httpLink,
      ),
    );

    
    return GraphQLProvider(
      client: client,
      child: CacheProvider(
        child: MaterialApp(
          title: 'GraphQL Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.orange,
          ),
          home: MyHomePage(),
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
   GraphQLConfiguration graphQLConfiguration = GraphQLConfiguration();
   var mq ='''
query Myquery {
  continents {
    name
  }
}
''';  

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Hasura Vote'),
      ),
      body: Container(
        child: Query(
          options: QueryOptions(
            documentNode: gql(mq),
            pollInterval: 4,
          ),

          builder: (QueryResult result,
              {VoidCallback refetch, FetchMore fetchMore}) {
                if(result.data==null){
                  return   Text('data is null');
                }
            if (result.loading) {
              print('isloading');
              return const Center(
                child: CircularProgressIndicator(),
              );
            }

            if (result.hasException) {
              return Text('\nErrors: \n  ' + result.exception.toString());
            }

            // result.data can be either a [List<dynamic>] or a [Map<String, dynamic>]
            final List<dynamic> pls = result.data['programming_language'];

            return ListView.builder(
              itemCount: pls.length,
              itemBuilder: (BuildContext context, int index) {
                final Map<String, dynamic> pl = pls[index];
                final String name = pl["name"];
                final int voteCount = pl["vote_count"];

                return Mutation(
                  options: MutationOptions(
                    documentNode: gql(mutations.vote),
                  ),
                  builder: (
                    RunMutation vote,
                    QueryResult voteResult,
                  ) {
                    if (voteResult.data != null && voteResult.data.isNotEmpty) {
                      pl['vote_count'] = pl['vote_count'] + 1;
                    }

                    return ListTile(
                      title: Text('$name - $voteCount'),
                      trailing: Icon(Icons.thumb_up),
                      onTap: () {
                        vote(<String, String>{
                          'name': name,
                        });
                      },
                    );
                  },
                );
              },
            );
          },
        ),
      ),
    );
  }
 
} 

@alinetskyi
Copy link

I also have the same issue!

@speller
Copy link

speller commented Mar 23, 2020

This issue happens when host lookup failed:

image

As you can see, the failure.address field is null.

@speller
Copy link

speller commented Mar 23, 2020

As a temporary solution, you may manually edit the problematic line until we get an update from authors:
host: failure.address != null ? failure.address.host : null,

@knaeckeKami
Copy link
Contributor

this is already fixed on the current beta release 3.0.1-beta.3

@staaouat
Copy link

staaouat commented Mar 24, 2020

The host failure problem is solved but the example still not working due the connection problem.

error: ClientException: Failed to connect to https://countries.trevorblades.com/

@staaouat
Copy link

Can somebody point me to a worked example.

@staaouat
Copy link

staaouat commented Apr 1, 2020

Finally figured out that it was because I had my VPN on.

@Aljabri-Salman
Copy link

Same issue . Could you show how to install the beta package?

@kn4rfy
Copy link

kn4rfy commented Jun 5, 2020

Adding this in my android manifest file solves my problem. I haven't checked with iOS yet

<uses-permission android:name="android.permission.INTERNET" />

@binaris-no
Copy link

@Aljabri-Salman Probably too late as a tip for you but it might help others to know how to get onto the beta version/track given the severity of this issue (in the stable version).
https://pub.dev/packages/graphql/versions/3.1.0-beta.7#-installing-tab-
(Or https://pub.dev/packages/graphql_flutter/versions/3.1.0-beta.7#-installing-tab- if you use the full package.)

Otherwise I reproduce it simply by starting an app that makes an API call soon after, while the network is down. This is basically a simulation of a case where an app is started on mobile while for whatever reason their is no internet connection / the network is down.

@muhrahmatullah
Copy link

also experiencing the same issue when testing using emulator but it works on real device, might be some connection problem

@micimize
Copy link
Collaborator

micimize commented Jul 1, 2020

@muhrahmatullah take a look at #212

@muhrahmatullah
Copy link

@micimize checked the issue but the sample I am trying to use is the github_bloc one. I think it doesn't have a correlation with setting the local host (?)

@micimize
Copy link
Collaborator

micimize commented Jul 4, 2020

@muhrahmatullah I just mean that if your real device is trying to connect to localhost or 10.0.2.2 it won't be able to, regardless of everything else. Might not be the actual issue you're having though

@andersonmendesdev
Copy link

I'm using the graphql: ^3.0.2 version and I also have this problem, I really need to catch this exception for offline internet. Any suggestion?

@muhrahmatullah
Copy link

@andersonmendesdev try use the beta version: graphql_flutter: ^3.1.0-beta.7

@andersonmendesdev
Copy link

@muhrahmatullah Yes, thank you, I'm already using it.

@speller
Copy link

speller commented Aug 5, 2020

For those who are subscribed to this thread and don't know yet - version 3.1.0 has been released with the fix of this issue.

@micimize
Copy link
Collaborator

micimize commented Aug 6, 2020

closing for now – if anyone has a similar issue please comment or open a new one

@micimize micimize closed this as completed Aug 6, 2020
@diegothucao
Copy link
Author

Thanks so much for your fixing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests