zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.25k stars 623 forks source link

Build error when importing graphql_flutter.dart #1213

Closed leonvisscher closed 2 years ago

leonvisscher commented 2 years ago

Describe the issue When importing the plugin into my flutter main file, building the project throws the following error:

: Error: 'cthrow' isn't a type.
../…/src/link.dart:172
      cthrow ServerException(
      ^^^^^^
: Error: Expected ';' after this.
../…/src/link.dart:172
      cthrow ServerException(
             ^^^^^^^^^^^^^^^
: Error: Expected ')' before this.
../…/src/link.dart:173
        originalException: e,
                         ^
: Error: The getter 'originalException' isn't defined for the class 'HttpLink'.
../…/src/link.dart:173
- 'HttpLink' is from 'package:gql_http_link/src/link.dart' ('../../flutter/.pub-cache/hosted/pub.dartlang.org/gql_http_link-0.4.2/lib/src/link.dart').
../…/src/link.dart:1
Try correcting the name to the name of an existing getter, or defining a getter or field named 'originalException'.
        originalException: e,
        ^^^^^^^^^^^^^^^^^

: Error: A non-null value must be returned since the return type 'Response' doesn't allow null.
../…/src/link.dart:166
- 'Response' is from 'package:http/src/response.dart' ('../../flutter/.pub-cache/hosted/pub.dartlang.org/http-0.13.5/lib/src/response.dart').
package:http/src/response.dart:1
  Future<http.Response> _executeRequest(Request request) async {

Failed to package /Users/user/projects/hello_world.
    Command PhaseScriptExecution failed with a nonzero exit code

To Reproduce (MUST BE PROVIDED)

  1. When adding the line import 'package:graphql_flutter/graphql_flutter.dart'; at the top of my simple main.dart file this error is thrown:
    
    import 'package:flutter/material.dart';
    import 'package:graphql_flutter/graphql_flutter.dart';

void main() async { runApp(MyApp()); }

class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Bird App', home: Container( child: Text('Hello World'), ), theme: ThemeData( appBarTheme: AppBarTheme( backgroundColor: Colors.blue, ), ), ); } }



This error is thrown when running `flutter run`. When I delete the import, there is no error

**Expected behavior**
No error

**device / execution context**
Running on iOS Emulator
vincenzopalazzo commented 2 years ago

This is fixed by this PR https://github.com/zino-hofmann/graphql-flutter/pull/1197

to solve this you should fix your dependencies of the link package to a major version, or maybe we should release a new beta version with this change included!

cc @budde377

knaeckeKami commented 2 years ago

Could you share your dependencies? This should not happen unless you use dependency overrides to gql_link 0.5.0

vincenzopalazzo commented 2 years ago

This happens if you clone a new project today I think, by the following rules gql_link: ^0.4.2

This is one of the reasons that I try to be reluctant to introduce breaking change often! We like to hack around and it is fine but a common user do this gql_link: 0.4.2.

There problem is gone, and also the user!

leonvisscher commented 2 years ago

@knaeckeKami

My pubspec.yaml looks like this:

name: flutter_complete_guide
description: A new Flutter project.

publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  graphql_flutter: ^5.0.0
  google_sign_in: ^5.4.1
  sign_in_with_apple: ^4.1.0
  flutter_signin_button: ^2.0.0
  flutter_hooks: ^0.18.5+1
  flutter_secure_storage: ^6.0.0

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true

  assets:
    - assets/
knaeckeKami commented 2 years ago

The app runs fine with the provided pubspec.yaml and main.dart for me. Could you try

leonvisscher commented 2 years ago

@knaeckeKami

Running flutter pub cache repair fixed the problem, thanks! 😄

I already tried flutter clean, pod deintegrate && pod install, ... commands and building a new project and only copying the lib/** files but that didn't work. Why does flutter pub cache repair do the job?

vincenzopalazzo commented 2 years ago

The easy solution is to share the version that you have of gql_link, if it is == 5.0.0, you need to override the dependence with : 0.4.2 and all will work fine

knaeckeKami commented 2 years ago

@leonvisscher : to me, it looks like you accidentally edited the file from gql_http_link manually after it it was downloaded by pub.dev, introducing a syntax error. Can happen if you inspected the source code from the imported package and the IDE is not configured to prevent you from editing files which don't belong to the project. Another explanation would be file system corruption (unlikely though as this usually changes characters, it does not add or remove them).

@vincenzopalazzo: Unless something goes horribly wrong, pub should never choose gql_link 0.5.0 (with the breaking change) when using the graphql package.

Since graphql depends on gql_link ^0.4.2, 0.5.0 is considered an incompatible version.

Trying to depend on it manually confirms that:

Because no versions of graphql_flutter match >5.1.0 <5.1.1-beta.1 and graphql_flutter >=5.0.2-beta.6 <5.1.0 depends on graphql ^5.0.2-beta.6, graphql_flutter >=5.0.2-beta.6 <5.1.0-∞ or >5.1.0 <5.1.1-beta.1 requires graphql ^5.0.2-beta.6.
And because graphql_flutter >=5.1.1-beta.1 <5.1.1-beta.2 depends on graphql ^5.1.1-beta.1, graphql_flutter >=5.0.2-beta.6 <5.1.0-∞ or >5.1.0 <5.1.1-beta.2 requires graphql ^5.0.2-beta.6.
And because graphql_flutter >=5.1.1-beta.2 depends on graphql ^5.1.2-beta.1 and graphql_flutter >=5.0.0-nullsafety.5 <5.0.1-beta.2 depends on gql_exec ^0.3.0, graphql_flutter >=5.0.0-nullsafety.5 <5.0.1-beta.2 or >=5.0.2-beta.6 <5.1.0-∞ or >5.1.0 requires gql_exec ^0.3.0 or graphql ^5.0.2-beta.6.
And because graphql_flutter >=5.0.1-beta.2 <5.0.2-beta.6 depends on gql_exec 0.3.0 and gql_link >=0.5.0 <0.5.1-alpha+1659917084473 depends on gql_exec ^0.4.0, if gql_link >=0.5.0 <0.5.1-alpha+1659917084473 and graphql_flutter >=5.0.0-nullsafety.5 <5.1.0-∞ or >5.1.0 then graphql ^5.0.2-beta.6.
And because graphql_flutter 5.1.0 depends on graphql ^5.1.0 which depends on gql_link ^0.4.2, gql_link >=0.5.0 <0.5.1-alpha+1659917084473 is incompatible with graphql_flutter >=5.0.0-nullsafety.5.
So, because flutter_complete_guide depends on both graphql_flutter ^5.0.0 and gql_link 0.5.0, version solving failed.
pub get failed (1; So, because flutter_complete_guide depends on both graphql_flutter ^5.0.0 and gql_link 0.5.0, version solving failed.)

I agree that we should be careful with breaking changes, but I think in this instance users should not be affected by that unless they use dependency overrides.

vincenzopalazzo commented 2 years ago

@vincenzopalazzo: Unless something goes horribly wrong, pub.dev should never choose gql_link 0.5.0 (with the breaking change) when using the graphql package.

Ah, ah you have right :D