trevorwang / retrofit.dart

retrofit.dart is an dio client generator using source_gen and inspired by Chopper and Retrofit.
https://mings.in/retrofit.dart/
MIT License
1.08k stars 251 forks source link

[retrofit_generator] Null check operator used on a null value again #631

Open Carapacik opened 1 year ago

Carapacik commented 1 year ago

Same as #630 https://github.com/Carapacik/swagger_parser/issues/110 With this code

import 'package:dio/dio.dart';
import 'package:retrofit/retrofit.dart';

part 'upload_client.g.dart';

@RestApi()
abstract class UploadClient {
  factory UploadClient(Dio dio, {String? baseUrl}) = _UploadClient;

  @POST('/api/upload/ossSignNew')
  Future<void> ossSignNewUsingPost({
    @Body() required Object model,
  });
}
[SEVERE] retrofit_generator on lib/api/upload/upload_client.dart:

Null check operator used on a null value
trevorwang commented 1 year ago

Please try dynamic instead of object here

trevorwang commented 1 year ago

Object is the base class for all dart objet. It will crash when call the below method on it.

  /// Returns `true` if representing a super class of [element].
  ///
  /// This check only takes into account the *extends* hierarchy. If you wish
  /// to check mixins and interfaces, use [isAssignableFrom].
  bool isSuperOf(Element element) {
    if (element is InterfaceElement) {
      var theSuper = element.supertype;

      do {
        if (isExactlyType(theSuper!)) {
          return true;
        }

        theSuper = theSuper.superclass;
      } while (theSuper != null);
    }

    return false;
  }

Try to fix it later

titabash commented 9 months ago

I encountered the same error.

Zvnimir commented 3 months ago

I encountered the same error, however, I am not using Object in my code.

Code:

import 'package:dio/dio.dart';
import 'package:myapp/domain/models/post.dart';
import 'package:retrofit/retrofit.dart';

part 'wishlist_api.g.dart';

@RestApi()
abstract class _WishlistAPI {
  factory _WishlistAPI(Dio dio) = __WishlistAPI;

  @GET('/me/wishlist/articles')
  Future<List<Article>> _getWishlist({
    @Query('orderBy[auto]') String? orderBy,
    @Query('offset') int? offset,
    @Query('limit') int? limit,
  });

  @POST('/me/wishlist/articles/{id}')
  Future<Article> toggleWishlistArticle({
    @Path('id') required String id,
  });
}

class WishlistAPI extends __WishlistAPI {
  WishlistAPI(Dio dio) : super(dio);

  Future<List<Article>> getWishlist({
    int? offset,
    int? limit,
  }) async {
    return _getWishlist(
      orderBy: 'ASC',
      offset: offset,
      limit: limit,
    );
  }
}

Error:

[SEVERE] retrofit_generator on lib/domain/remote/apis/wishlist_api.dart:

Null check operator used on a null value
package:source_gen/src/type_checker.dart 215:74             TypeChecker.isSuperTypeOf
package:retrofit_generator/src/generator.dart 872:51        RetrofitGenerator._generateRequest
package:retrofit_generator/src/generator.dart 410:17        RetrofitGenerator._generateMethod.<fn>
package:code_builder/src/specs/method.g.dart 323:33         _$MethodBuilder.update
package:code_builder/src/specs/method.g.dart 38:29          new _$Method
package:retrofit_generator/src/generator.dart 360:12        RetrofitGenerator._generateMethod
package:retrofit_generator/src/generator.dart 216:21        RetrofitGenerator._parseMethods.<fn>
dart:core                                                   List.addAll
package:built_collection/src/list/list_builder.dart 98:14   ListBuilder.addAll
package:retrofit_generator/src/generator.dart 112:19        RetrofitGenerator._implementClass.<fn>
package:code_builder/src/specs/class.g.dart 345:33          _$ClassBuilder.update
package:code_builder/src/specs/class.g.dart 40:28           new _$Class
package:retrofit_generator/src/generator.dart 102:26        RetrofitGenerator._implementClass
package:retrofit_generator/src/generator.dart 87:12         RetrofitGenerator.generateForAnnotatedElement
package:source_gen/src/generator_for_annotation.dart 61:30  GeneratorForAnnotation.generate
package:source_gen/src/builder.dart 342:33                  _generate
dart:async                                                  Stream.toList.<fn>
package:source_gen/src/builder.dart 107:9                   _Builder._generateForLibrary
package:source_gen/src/builder.dart 99:5                    _Builder.build

Versions: retrofit: ^4.1.0 retrofit_generator: ^8.1.1

Zvnimir commented 3 months ago

I managed to fixed it, but it is probably not the best solution.

Set retrofit_generator version to ^7.0.8.

Added this to the build.yaml file:

global_options:
  freezed:
    runs_before:
      - json_serializable
  json_serializable:
    runs_before:
      - retrofit_generator